in reply to Blushing at the facts
in thread Simple Rounding
and$var1 = sprintf "%.2f" => $number;
$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?$var2 = int ($number * 100 + .5) / 100;
See perldoc -q decimal.
|
|---|