Skip to content

Core\Decimal::divideExact

Divides one number by another and throws an error when the answer is not exact.

Core\Decimal::divideExact returns the result when it can be written as a decimal, and throws an ArithmeticError when it cannot. 10 / 4 is 2.5, so that call returns 2.5. 10 / 3 is 3.333… with no end, so that call throws. Dividing by zero throws as well.

Use it where your program already assumes the division comes out even. Examples are splitting a total between a known number of people, and converting a quantity between two units. Another is working out a unit price from a pack price. In each case, a rounded answer would be a mistake that nobody sees. Core\Decimal::divideRounded is the division to use when rounding is what you want. It takes how many digits to keep, and how to round a value that is exactly between two results.

Good to know: the result has only the digits it needs. 10.00 / 4 returns 2.5. It does not return 2.50.

Parameters

  • $valuedecimalThe dividend.
  • $divisordecimalWhat to divide it by.

Return value

Returns decimal. The result, with only as many digits after the point as it needs. 1.00 / 4 is 0.25.

Errors

  • ArithmeticErrorWhen $divisor is zero, when the result has repeating digits, or when it does not fit in a decimal. The last two cases have the same message.

Changelog

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

Examples

Divide when it comes out even

<?nvs
// Shows how to divide two numbers and get an exact answer. A bill is split
// between four people, and each share is a whole number of cents.
decimal $bill = 84.00;
decimal $people = 4;
// This prints 21.
echo Core\Decimal::divideExact($bill, $people), "\n";
// The answer keeps only the digits it needs. 10 divided by 4 is 2.5, so this
// prints 2.5 and not 2.50.
echo Core\Decimal::divideExact(10.00, 4.00), "\n";
// A negative number divides the same way. This prints -21.
echo Core\Decimal::divideExact(-$bill, $people), "\n";
Output
21
2.5
-21

Catch a division that is not exact

<?nvs
// Shows what happens when a division does not come out even. The error you
// catch is `ArithmeticError`, and its message says what could not be answered.
decimal $bill = 84.00;
array<decimal> $people = [4, 9, 0];
foreach ($people as decimal $count) {
try {
decimal $share = Core\Decimal::divideExact($bill, $count);
echo 'each of ', $count, ' pays ', $share, "\n";
} catch (ArithmeticError $notExact) {
// 84 divided by 9 repeats forever, and nothing can be divided by zero.
echo $notExact->message, "\n";
}
}
Output
each of 4 pays 21
Core\Decimal::divideExact cannot answer 84.00 / 9 exactly: the quotient repeats or is wider than a `decimal` holds
Core\Decimal::divideExact cannot divide by zero

A unit price from a pack price

<?nvs
// Shows how to work out what one item in a pack costs. When the pack price does
// not divide evenly, there is no exact unit price.
array<decimal> $packPrice = ['tea' => 7.20, 'soap' => 4.50, 'rice' => 10.00];
array<decimal> $packSize = ['tea' => 8, 'soap' => 3, 'rice' => 3];
foreach ($packPrice as string $name => decimal $price) {
try {
decimal $each = Core\Decimal::divideExact($price, $packSize[$name]);
echo $name, ': ', $each, " each\n";
} catch (ArithmeticError $uneven) {
// The unit price would have digits with no end. To get a rounded unit
// price instead, use `Core\Decimal::divideRounded`.
echo $name, ": no exact unit price\n";
}
}
Output
tea: 0.9 each
soap: 1.5 each
rice: no exact unit price