asago
asked on
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")???
that is over three digits, (1234) to (1,234)
printf("%1.1d")???
ASKER
Ok - not what I wanted to hear because I am not using Perl, long story, but oh well. Thanks - please submit as an answer.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks.
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.