Skip to content

Core\Decimal::divideRounded

Divides one number by another and rounds the answer to as many digits as you ask for.

Core\Decimal::divideRounded takes four arguments: the value, the divisor, how many digits to keep after the point, and how to round. 100 divided by 3 at two digits is 33.33. The result always has as many digits after the point as you asked for, so 100 divided by 4 at two digits is 25.00. Dividing by zero throws an ArithmeticError, and so does a scale above 28.

Use it where rounding is part of the rule your program follows. Examples are a bill split between people, a price converted into another currency, and a rate applied to an amount. The fourth argument is a Core\RoundMode case. It says how to round a value that is exactly between two results. Core\Decimal::divideExact is the division to use when the answer has to come out even.

Good to know: the / operator also rounds. A value exactly between two results goes to the even one, and you cannot change that. Use divideRounded when your program must choose the number of digits or the rounding.

Parameters

  • $valuedecimalThe dividend.
  • $divisordecimalWhat to divide it by.
  • $scaleuintThe number of digits after the decimal point, at most 28. The result always has exactly this many, so a rounded price keeps its trailing zeros.
  • $modeCore\RoundModeHow a value exactly between two results is rounded. It is the same Core\RoundMode that Core\Math::round takes. The division is exact, and its result is rounded only once.

Return value

Returns decimal. The result, with exactly $scale digits after the point.

Errors

  • ArithmeticErrorWhen $divisor is zero, when $scale is more than 28, or when the result does not fit in a decimal with that many digits after the point.

Changelog

VersionChange
0.0.6Core\Decimal::divideExact, divideRounded and power write their words in full. The old names were divExact, divRound and pow.

Examples

A share rounded to cents

<?nvs
// Shows how to divide a number and keep a fixed number of digits. A bill is
// split between three people, and each share is rounded to cents.
decimal $bill = 100.00;
// The third argument is how many digits to keep after the point. This
// prints 33.33.
echo Core\Decimal::divideRounded($bill, 3, 2, Core\RoundMode::HalfUp), "\n";
// The answer always has the number of digits you asked for. 100 divided by 4
// is 25, so this prints 25.00.
echo Core\Decimal::divideRounded($bill, 4, 2, Core\RoundMode::HalfUp), "\n";
// Four digits after the point. This prints 33.3333.
echo Core\Decimal::divideRounded($bill, 3, 4, Core\RoundMode::HalfUp), "\n";
Output
33.33
25.00
33.3333

The mode decides a tie

<?nvs
// Shows what the fourth argument does. 1 divided by 8 is 0.125. At two digits
// that value sits exactly between 0.12 and 0.13, and each mode settles it in
// its own way.
array<Core\RoundMode> $modes = [
Core\RoundMode::HalfUp,
Core\RoundMode::HalfDown,
Core\RoundMode::HalfEven,
Core\RoundMode::HalfOdd,
Core\RoundMode::Up,
Core\RoundMode::Down,
];
// This prints 0.13, 0.12, 0.12, 0.13, 0.13 and 0.12, one on each line.
foreach ($modes as Core\RoundMode $mode) {
echo Core\Decimal::divideRounded(1, 8, 2, $mode), "\n";
}
Output
0.13
0.12
0.12
0.13
0.13
0.12

Prices in another currency

<?nvs
// Shows how to put a price list into another currency. Every price is divided
// by the rate and rounded to cents, which is what an invoice prints.
array<decimal> $prices = ['desk' => 249.00, 'lamp' => 39.90, 'chair' => 115.50];
decimal $rate = 1.0875;
decimal $sum = 0.00;
foreach ($prices as string $item => decimal $price) {
decimal $converted = Core\Decimal::divideRounded($price, $rate, 2, Core\RoundMode::HalfUp);
$sum = $sum + $converted;
echo $item, ': ', $converted, "\n";
}
// Add the rounded prices, never the unrounded ones. Then the total on the
// invoice is the total of the lines the customer reads. This prints 371.87.
echo 'total: ', $sum, "\n";
Output
desk: 228.97
lamp: 36.69
chair: 106.21
total: 371.87