[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Printf and commas

Posted on 1998-02-06
4
Medium Priority
?
1,866 Views
Last Modified: 2008-03-10
What is the format for placing commas in an integer
that is over three digits, (1234) to (1,234)
printf("%1.1d")???
0
Comment
Question by:asago
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1209723
From the perlFAQ:

How can I output my numbers with commas added?

This one will do it for you:

    sub commify {
        local $_  = shift;
        1 while s/^(-?\d+)(\d{3})/$1,$2/;
        return $_;
    }

    $n = 23659019423.2331;
    print "GOT: ", commify($n), "\n";

    GOT: 23,659,019,423.2331

You can't just:

    s/^(-?\d+)(\d{3})/$1,$2/g;

because you have to put the comma in and then recalculate your position.

0
 

Author Comment

by:asago
ID: 1209724
Ok - not what I wanted to hear because I am not using Perl, long story, but oh well.   Thanks - please submit as an answer.
0
 
LVL 84

Accepted Solution

by:
ozo earned 200 total points
ID: 1209725
If this was a question about the C printf function, it may have been better to ask it in the C topic area.
(where you'd probably be told that there is no standard format to do it,
but someone may know of a specific C implementation which has added it as an extension)
You may be told about localeconv, which can indicate which characters whoud be
appropriate to use for such formats in your locale.
And perhaps someone there would even know a C function to do what you want.
0
 

Author Comment

by:asago
ID: 1209726
Thanks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question