Skip to content

Core\Decimal::round

Rounds a number to as many digits as you ask for, in the direction you name.

Core\Decimal::round takes the value, a Core\RoundMode case, and how many digits to keep after the point. Leaving the last one out gives a whole number. The mode decides where a value that sits exactly between two neighbours goes, so 0.125 at two digits is 0.13 under Core\RoundMode::HalfUp and 0.12 under Core\RoundMode::HalfEven. The answer is exact, because it is a decimal and not a floating-point number. A scale above 28 throws an ArithmeticError.

Use it where the rounding rule belongs to your program: a tax line, a price a shop prints, a figure a report states at one digit.

Good to know: Core\Decimal::floor, Core\Decimal::ceil and Core\Decimal::truncate each move in one fixed direction and take no mode. Name a mode here when the direction is part of your rule.

Parameters

  • $valuedecimalThe value to round.
  • $modeCore\RoundModeHow a value exactly between two results is rounded. It is the same Core\RoundMode that Core\Decimal::divideRounded and Core\Math::round take. It is required, so it comes before the optional $scale.
  • $scaleuintdefault 0The number of digits after the decimal point, at most 28. Without it, the result has none. The result always has exactly this many, so a shorter value is padded with zeros.

Return value

Returns decimal. The rounded value with $scale digits. round(0.125, Core\RoundMode::HalfEven, 2) is 0.12, and round(0.125, Core\RoundMode::HalfUp, 2) is 0.13.

Errors

  • ArithmeticErrorWhen $scale is more than 28. Also when $value has too many digits before the point to keep that many digits after it.

Examples

Round to the digits you name

<?nvs
// Shows how to round a number. The mode comes before the number of digits,
// because the mode is always needed and the digits are not.
decimal $value = 0.125;
// Two digits are kept. 0.125 sits exactly between 0.12 and 0.13, and
// `HalfUp` sends it to the larger one. This prints 0.13.
echo Core\Decimal::round($value, Core\RoundMode::HalfUp, 2), "\n";
// Without the last argument the answer is a whole number. This prints 0.
echo Core\Decimal::round($value, Core\RoundMode::HalfUp), "\n";
// The answer always has the number of digits you asked for, so this
// prints 0.1250.
echo Core\Decimal::round($value, Core\RoundMode::HalfUp, 4), "\n";
Output
0.13
0
0.1250

Away from zero or towards it

<?nvs
// Shows the two modes that ignore which neighbour is nearer. `Up` moves away
// from zero and `Down` moves towards it, whatever the digits say.
array<decimal> $values = [2.1, 2.9, -2.1, -2.9];
// This prints 3, 3, -3 and -3, one on each line.
foreach ($values as decimal $value) {
echo Core\Decimal::round($value, Core\RoundMode::Up), "\n";
}
// The same four values under `Down`. This prints 2, 2, -2 and -2.
foreach ($values as decimal $value) {
echo Core\Decimal::round($value, Core\RoundMode::Down), "\n";
}
Output
3
3
-3
-3
2
2
-2
-2

Tax on an invoice line

<?nvs
// Shows how to work out the tax on each line of an invoice. The shop's rule
// says the tax is rounded to cents, and a half cent goes up.
array<decimal> $lines = ['desk' => 249.95, 'lamp' => 34.50, 'cable' => 7.25];
decimal $rate = 0.19;
decimal $tax = 0.00;
foreach ($lines as string $item => decimal $price) {
decimal $line = Core\Decimal::round($price * $rate, Core\RoundMode::HalfUp, 2);
echo $item, ': ', $line, "\n";
$tax = $tax + $line;
}
echo 'tax: ', $tax, "\n";
Output
desk: 47.49
lamp: 6.56
cable: 1.38
tax: 55.43