Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting currency - with comma

Status
Not open for further replies.

egrant

Programmer
Mar 6, 2003
42
0
0
AU
Hi All,

I need to add a comma to my totals between the thousands ie:
convert 1000.00 into 1,000.00
convert 10000.00 into 10,000.00
convert 100000.00 into 100,000.00

I find sprintf will add the decimal, but I can't get it to add the comma.

Any help would be great!

Em
 
Here you go..

Code:
#!/usr/bin/perl -w

use strict;

my $ammount = "123456789.00";  

my $new_format = commify($ammount);
print "$new_format\n";

sub commify {
        local $_  = shift;

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

M. Brooks
X Concepts LLC
 
From a while ago on the Perl Forum here
From


sub commify
{
my $text = reverse $_[0];
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
return scalar reverse $text;
}


HTH
--Paul




It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
try looking for the number::format module on cpan. (seach.cpan.org) It worked just fine for me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top