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