Keep It Simple, Stupid | |
PerlMonks |
Re^3: Rounding off numbersby Athanasius (Archbishop) |
on Sep 13, 2014 at 09:34 UTC ( [id://1100478]=note: print w/replies, xml ) | Need Help?? |
Hello jmichae3, and welcome to the Monastery! As swampyankee said, int($x + 0.5) correctly rounds $x to an integer, provided that $x is non-negative. So, for example, if you have 2 * 4.5 and want to make sure this comes out to 9, use:
This is useful, because for some values (and on some machines), a calculation like 2 * 4.5 might come out as 8.99999999998. But with the formula: add 0.5 and truncate, that’s OK now:
Hope that helps,
In Section
Seekers of Perl Wisdom
|
|