Litipk\BigNumbers\Decimal::div PHP Méthode

div() public méthode

Warning: div with $scale == 0 is not the same as integer division because it rounds the last digit in order to minimize the error.
public div ( Decimal $b, integer $scale = null ) : Decimal
$b Decimal
$scale integer
Résultat Decimal
    public function div(Decimal $b, $scale = null)
    {
        self::paramsValidation($b, $scale);
        if ($b->isZero()) {
            throw new \DomainException("Division by zero is not allowed.");
        } elseif ($this->isZero() || $b->isInfinite()) {
            return DecimalConstants::Zero();
        } else {
            if ($scale !== null) {
                $divscale = $scale;
            } else {
                // $divscale is calculated in order to maintain a reasonable precision
                $this_abs = $this->abs();
                $b_abs = $b->abs();
                $log10_result = self::innerLog10($this_abs->value, $this_abs->scale, 1) - self::innerLog10($b_abs->value, $b_abs->scale, 1);
                $divscale = (int) max($this->scale + $b->scale, max(self::countSignificativeDigits($this, $this_abs), self::countSignificativeDigits($b, $b_abs)) - max(ceil($log10_result), 0), ceil(-$log10_result) + 1);
            }
            return self::fromString(bcdiv($this->value, $b->value, $divscale + 1), $divscale);
        }
    }

Usage Example

Exemple #1
0
 /**
  * Returns a new Money object that represents
  * the divided value by the given factor
  *
  * @param numeric $divisor
  * @param int $roundingMode
  * @param int $precision
  * @throws Exception\InvalidArgumentException If division by zero accured
  * @return Money
  */
 public function divide($divisor, $roundingMode = self::ROUND_HALF_UP, $precision = self::PRECISION_GAAP)
 {
     $this->assertRoundingMode($roundingMode);
     $divisor = $this->castOperand($divisor);
     $precision = $this->castPrecision($precision);
     $innerPrecision = $this->getInnerPrecision();
     if ($divisor->isZero($innerPrecision)) {
         throw new Exception\InvalidArgumentException('Division by zero');
     }
     $amount = $this->amount->div($divisor, $innerPrecision + 1);
     if (null === $roundingMode) {
         $amount = $amount->round($precision);
     } else {
         $amount = Math::bcround($amount, $precision, $roundingMode);
     }
     return $this->newInstance($amount);
 }
All Usage Examples Of Litipk\BigNumbers\Decimal::div