• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1876
  • Last Modified:

Printf and commas

What is the format for placing commas in an integer
that is over three digits, (1234) to (1,234)
printf("%1.1d")???
0
asago
Asked:
asago
  • 2
  • 2
1 Solution
 
ozoCommented:
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
 
asagoAuthor Commented:
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
 
ozoCommented:
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
 
asagoAuthor Commented:
Thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now