Forum Moderators: coopster & phranque

Message Too Old, No Replies

Formatting "Precision"

Controlling number of decimal places

         

Conan_of_Oz

8:48 pm on Sep 11, 2005 (gmt 0)

10+ Year Member



My script generates a dollar value which I want to display, naturally, to 2 decimal places.

When I display this value within a table, it crops trailing zero's, so a value of $19.90 displays as $19.9

How can I control this?

NOTE: The section in question is in HTML format, using cgi variables. It is contained within a cgi "print block".

KevinADC

9:08 pm on Sep 11, 2005 (gmt 0)

10+ Year Member



you could do it with a rewgexp, but using sprintf is easy too:

not sure how you are generating the dollar figures, but here's a generic example:



my @figures = (19.9, 100.000, 123.093, 34);
foreach my $line (@figures) {
$line = sprintf("%.2f", $line);
print "\$$line\n";
}

prints:

$19.90
$100.00
$123.09
$34.00

basically, just pass the variable you want to format to the sprinf() function. sprintf() has many common string formatting options:

[perldoc.perl.org...]