[ back to toc ]

Decimal Places

Date: 2002/06/12 16:49

Q:
Hi,
I have a Perl script, which works out the percentage of a number. However,
it often produces a number with 3 decimal places like 1.123, which I do
not want.
So, is there any way of either making Perl round the number up or down to
the nearest number so 1.123 would become either 1.12 or 1.13, or, making
it only produce the figure with 2 decimal places?
Thank you for your help.
*NAME-DELETED*
A:
Any time in Perl you can use a variable as it was a string. It is
automatically converted. This way you can say:

$a = 1/3;
$a =~ s/\..*$//;
print $a;

and you have chopped off all fractions after the dot.

regards,
Peter

[ back to toc ]