div() public method

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
return Decimal
Esempio n. 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);
 }
 private function floorModulo(Decimal $x, Decimal $y) : Decimal
 {
     return $x->sub($y->mul($x->div($y)->floor()));
 }