Skip to content

Core\RoundMode

How a number that lands exactly between two neighbours is settled when you round it.

Four of the six rules only decide a tie — a value like 2.5, where neither neighbour is closer. HalfUp sends it away from zero, and is what you get when you say nothing. HalfDown sends it toward zero. HalfEven sends it to the even neighbour, which is what a long column of money or measurements usually wants, because it does not drift the way always-up does. HalfOdd sends it to the odd neighbour. The other two ignore ties and move every value that has anything to discard: Up away from zero, Down toward it.

Good to know: all six work at any precision, so they can settle the second decimal, not only the whole number. They are about distance from zero rather than about the number line, so Up on a negative value moves it further below zero.

Examples

The six rules on one tie

<?nvs
// This program rounds the same numbers with all six rounding modes.
// A tie is a value exactly halfway between two whole numbers. 2.5 is as far from 2 as from 3.
// Each mode rounds a tie in its own way.
class Rules
{
public static function label(Core\RoundMode $rule): string
{
return match ($rule) {
Core\RoundMode::HalfUp => 'HalfUp ',
Core\RoundMode::HalfDown => 'HalfDown',
Core\RoundMode::HalfEven => 'HalfEven',
Core\RoundMode::HalfOdd => 'HalfOdd ',
Core\RoundMode::Up => 'Up ',
Core\RoundMode::Down => 'Down ',
};
}
}
array<Core\RoundMode> $rules = [
Core\RoundMode::HalfUp,
Core\RoundMode::HalfDown,
Core\RoundMode::HalfEven,
Core\RoundMode::HalfOdd,
Core\RoundMode::Up,
Core\RoundMode::Down,
];
foreach ($rules as Core\RoundMode $rule) {
echo Rules::label($rule),
' 2.5 -> ', Core\Math::round(2.5, { mode: $rule }),
' -2.5 -> ', Core\Math::round(-2.5, { mode: $rule }),
"\n";
}
// 2.4 is not a tie, so the four `Half` modes all round it to 2.
// `Up` always rounds away from zero, so it gives 3. `Down` always rounds toward zero.
echo "\n";
foreach ($rules as Core\RoundMode $rule) {
echo Rules::label($rule), ' 2.4 -> ', Core\Math::round(2.4, { mode: $rule }), "\n";
}
Output
HalfUp 2.5 -> 3 -2.5 -> -3
HalfDown 2.5 -> 2 -2.5 -> -2
HalfEven 2.5 -> 2 -2.5 -> -2
HalfOdd 2.5 -> 3 -2.5 -> -3
Up 2.5 -> 3 -2.5 -> -3
Down 2.5 -> 2 -2.5 -> -2
HalfUp 2.4 -> 2
HalfDown 2.4 -> 2
HalfEven 2.4 -> 2
HalfOdd 2.4 -> 2
Up 2.4 -> 3
Down 2.4 -> 2

Settling a decimal place and not just a whole number

<?nvs
// A rule settles whichever place you are rounding to, not only the whole
// number. 0.125 is an exact tie at two decimals: it sits halfway between 0.12
// and 0.13, so the rule decides which one you get.
echo 'half up ', Core\Math::round(1.125, { precision: 2, mode: Core\RoundMode::HalfUp }), "\n";
echo 'half even ', Core\Math::round(1.125, { precision: 2, mode: Core\RoundMode::HalfEven }), "\n";
// The two rules that are not about ties move the value at that place whatever
// it is. `Up` is away from zero, so on a negative number it goes further down.
echo "\n";
echo 'up on 1.121 ', Core\Math::round(1.121, { precision: 2, mode: Core\RoundMode::Up }), "\n";
echo 'up on -1.121 ', Core\Math::round(-1.121, { precision: 2, mode: Core\RoundMode::Up }), "\n";
echo 'down on 1.129 ', Core\Math::round(1.129, { precision: 2, mode: Core\RoundMode::Down }), "\n";
// A precision below zero rounds to the left of the point, which is how a
// figure is reported to the nearest hundred.
echo "\n";
echo 'to the nearest hundred ', Core\Math::round(1250.0, { precision: -2 }), "\n";
Output
half up 1.13
half even 1.12
up on 1.121 1.13
up on -1.121 -1.13
down on 1.129 1.12
to the nearest hundred 1300

A column of money that should not drift

<?nvs
// This program rounds invoice amounts to the cent with two different modes.
// Every amount ends in an exact half cent, so the mode changes the result.
// The two totals show the difference over four rows.
array<float> $rows = [1.125, 2.375, 3.625, 4.875];
float $alwaysUp = 0.0;
float $toTheEven = 0.0;
foreach ($rows as float $amount) {
// `HalfUp` always rounds a half cent up.
// Over many rows, the total becomes too large.
float $up = Core\Math::round($amount, { precision: 2, mode: Core\RoundMode::HalfUp });
// `HalfEven` rounds a half cent to the nearest even cent. About half of
// the amounts go down, so the total stays close to the exact sum.
float $even = Core\Math::round($amount, { precision: 2, mode: Core\RoundMode::HalfEven });
echo Core\Str::format('%.3f', $amount), ' half up ', Core\Str::format('%.2f', $up),
' half even ', Core\Str::format('%.2f', $even), "\n";
$alwaysUp = $alwaysUp + $up;
$toTheEven = $toTheEven + $even;
}
echo "\n";
echo 'total, half up ', Core\Str::format('%.2f', $alwaysUp), "\n";
echo 'total, half even ', Core\Str::format('%.2f', $toTheEven), "\n";
Output
1.125 half up 1.13 half even 1.12
2.375 half up 2.38 half even 2.38
3.625 half up 3.63 half even 3.62
4.875 half up 4.88 half even 4.88
total, half up 12.02
total, half even 12.00