how sub can have 2 decimal numbers only?

X

xman

my $usdTotal = $Total / 6.2;

when Perl calculate it I hzave result like $usdTotal = 120.2435445466

but I need 120.24 only (2 digits after dot..)

how I can do it?
 
A

A. Sinan Unur

xman said:
my $usdTotal = $Total / 6.2;

when Perl calculate it I hzave result like $usdTotal = 120.2435445466

but I need 120.24 only (2 digits after dot..)

how I can do it?

perldoc -f sprintf

Please consult the documentation first.

Sinan
 
M

Mike Deskevich

my $usdTotal = sprintf "%.2f",$Total/6.2;

or

my $usdTotal = int($Total/6.2*100)/100
 
T

Tad McClellan

xman said:
when Perl calculate it I hzave result like $usdTotal = 120.2435445466

but I need 120.24 only (2 digits after dot..)

how I can do it?


perldoc -q round

Does Perl have a round() function? What about ceil() and floor()?
Trig functions?
 
M

Mike Deskevich

also, i forgot to take care of any rounding. ignoring any precision
errors, i should have said

$usdTotal=int(($Total/6.2*100)+.5)/100;

but you are correct about the precision errors. i forgot about that.
i always do my conversions with sprintf, so i never have to worry about
that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top