[thelist] Adding extra zeros to dollar amounts when neccesary

Kevin webmaster at websitesupdate.com
Thu Feb 27 23:45:17 CST 2003



> Would anyone be able to outline a method (in ECMA/javascript ideally) by
> which I could add extra zeros to a numeric value to display a dollar
amount
> correctly?
>
> Example: 5 into 5.00
>
> To someone who's only dabbled occasionally in client-side scripting, your
> help would be invaluable!
>
> Much thanks in advance,
>
> Ryan

Here is a perl version, if you decide to go with the server side route:


sub dollify ($) {
 my $text = reverse sprintf "%.02f", $_[0];
 $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
 return '$' . reverse $text;
}

print dollify(5);

> $5.00






More information about the thelist mailing list