PDA

View Full Version : How do i display and format numbers as money


NiteOwl
08-31-2006, 03:38 AM
Thanks for looking...

How do i check a number/string and output it as money

such as 3456
3,456

or

1234567
$1,234,567

The input range will be from 5000 to 10000000

Thanks,
NiteOwl

FishMonger
08-31-2006, 06:11 AM
http://search.cpan.org/~claco/Handel-0.33/lib/Handel/Currency.pm

NiteOwl
08-31-2006, 07:12 AM
thanks,
I looked at it and it was not really what i need.

I need something real simple if possable.

This code will produce $1234567.00


#!/perl/bin/perl

use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);


print "Content-type: text/html\n\n";

$money = 1234567;
$money = "\$".sprintf('%.2f',($money));

print $money;



#############

all i need now is the comma's


$###,###,###.## style

KevinADC
08-31-2006, 07:28 AM
$money = 1234567;
$money = sprintf('%.2f',($money));
$money = commify($money);
print $money;

sub commify {
(my $t = shift) =~ s/\G(\d{1,3})(?=(?:\d\d\d)+(?:\.|$))/$1,/g;
return "\$$t";
}

NiteOwl
08-31-2006, 07:37 AM
Thanks, exactly what i needed.
:thumbsup: