Solved

Printf and commas

Posted on 1998-02-06
4
1,859 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 50 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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

695 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