[EMAIL PROTECTED] wrote:
Hello,
Here's my question.
Let's say I have the following numbers and I want to print them out so they
are formatted in money terms:
examples:
10834.00
1939432.00
to print out as:
$10,834.00
$1,939,432.00
How can I do this? I was suspecting that the "printf" or "sprintf" functions
may accomplish this. But I was not able to figure it out. I was thinking
that I may have to build some kind of subroutine to accomplish this but I was
hoping there would simpler way. Can anyone help me?
Thanks in advance.
$ perldoc -q "numbers with commas"
How can I output my numbers with commas added?
This subroutine will add commas to your number:
sub commify {
local $_ = shift;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_;
}
This regex from Benjamin Goldberg will add commas to numbers:
s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))│\G\d{3}(?=\d))/$1,/g;
...
--jjv
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>