in reply to Rounding off numbers
Or, to avoid sprintf, you could use:
$rounded = int($unrounded + 0.5);
There are actually defined standards for rounding; to be absolutely pedantic, numbers which are exact odd multiples of ½ (i.e., (2n + 1)⁄/2) should round to the nearest odd number, so 4.5 and 5.5 should both round to 5
salva's post reminded me that my fragment will only work for positive values of $unrounded. For negative values, one would have to subtract 0.5
emc
"Being forced to write comments actually improves code, because it is easier to fix a crock than to explain it. "
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Rounding off numbers
by Limbic~Region (Chancellor) on Apr 20, 2006 at 16:46 UTC | |
by swampyankee (Parson) on Apr 20, 2006 at 22:11 UTC | |
by gloryhack (Deacon) on Apr 20, 2006 at 18:58 UTC | |
by johngg (Canon) on Apr 20, 2006 at 20:18 UTC | |
by Limbic~Region (Chancellor) on Apr 20, 2006 at 20:41 UTC | |
by gloryhack (Deacon) on Apr 21, 2006 at 15:05 UTC | |
|
Re^2: Rounding off numbers
by jmichae3 (Initiate) on Sep 13, 2014 at 08:51 UTC | |
by Athanasius (Archbishop) on Sep 13, 2014 at 09:34 UTC |