Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. BigDecimal.php

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\Math

Code

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();
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal