Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Blushing at the facts

by Abigail-II (Bishop)
on Mar 25, 2004 at 16:47 UTC ( [id://339799]=note: print w/replies, xml ) Need Help??


in reply to Blushing at the facts
in thread Simple Rounding

There is a subtle, but important difference between
$var1 = sprintf "%.2f" => $number;
and
$var2 = int ($number * 100 + .5) / 100;
$var1 is a PV, that is, the interval variable has a string value, but not a numeric value, while $var2 is an NV, that is, it has a numeric value, but not a string value. This means that $var1 will have two digits after the decimal point when printed - sprintf has garanteed that. But that's not necessarely true for $var2. Since you cannot represent 1/100 exactly in binary, you left the possibility open that if you stringify the number, you end up with more than 2 characters after the decimal point. Now, it may not happen in a thousand testcases, but can you guarantee it will never happen?

See perldoc -q decimal.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://339799]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-24 21:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found