[ back to toc ]

Currency format

Date: 2002/01/14 21:09

Q:
Good day Peter,
I am doing a project in Montreal. My employee asked me to format all my
digites in the currency format for example if I have 1233.211326
it has to be formated to $1233.21 so I need a function to do this for me.
Can I use regular expresion to do this for me.
any help appriciated.
Best Regards,
*NAME-DELETED*
A:
print &CurrFormat(1233.2114),"\n";
print &CurrFormat(12332.3266),"\n";
print &CurrFormat(12322),"\n";
print &CurrFormat(1233234.5544),"\n";
print &CurrFormat(.666),"\n";

sub CurrFormat {
my $num = shift;

$num = $num*100+0.5;
$num =~ s/\..*$//;
$num /= 100;
while( $num =~ /(\d+)(\d{3})(.*)$/ ){ $num = "$1,$2$3";}
$num = '$' . $num;
$num .= '.00' unless $num =~ /\./ ;
$num
}

[ back to toc ]