function BigDecimal::exactlyDividedBy
Returns the exact result of the division of this number by the given one.
The scale of the result is automatically calculated to fit all the fraction digits.
Parameters
BigNumber|int|float|string $that The divisor. Must be convertible to a BigDecimal.:
Throws
MathException If the divisor is not a valid number, is not convertible to a BigDecimal, is zero, or the result yields an infinite number of digits.
File
-
vendor/
brick/ math/ src/ BigDecimal.php, line 263
Class
- BigDecimal
- Immutable, arbitrary-precision signed decimal numbers.
Namespace
Brick\MathCode
public function exactlyDividedBy(BigNumber|int|float|string $that) : BigDecimal {
$that = BigDecimal::of($that);
if ($that->value === '0') {
throw DivisionByZeroException::divisionByZero();
}
[
,
$b,
] = $this->scaleValues($this, $that);
$d = \rtrim($b, '0');
$scale = \strlen($b) - \strlen($d);
$calculator = Calculator::get();
foreach ([
5,
2,
] as $prime) {
for (;;) {
$lastDigit = (int) $d[-1];
if ($lastDigit % $prime !== 0) {
break;
}
$d = $calculator->divQ($d, (string) $prime);
$scale++;
}
}
return $this->dividedBy($that, $scale)
->stripTrailingZeros();
}