/** * fromReal. * * @param Real $real * @param RoundingMode $roundingMode * * @return static */ public static function fromReal(Real $real, RoundingMode $roundingMode = null) { if (null === $roundingMode) { $roundingMode = RoundingMode::HALF_UP(); } $value = filter_var(round($real->value(), 0, $roundingMode->value()), FILTER_VALIDATE_INT); return new static($value); }
/** * Returns the integer part of the Real number as a Integer * * @param RoundingMode $rounding_mode Rounding mode of the conversion. Defaults to RoundingMode::HALF_UP. * @return Integer */ public function toInteger(RoundingMode $rounding_mode = null) { if (null === $rounding_mode) { $rounding_mode = RoundingMode::HALF_UP(); } $value = $this->toNative(); $integerValue = \round($value, 0, $rounding_mode->toNative()); $integer = new Integer($integerValue); return $integer; }
/** * fromReal. * * @param Real $real * @param RoundingMode $roundingMode * @param bool $force * * @return static */ public static function fromReal(Real $real, RoundingMode $roundingMode = null, $force = true) { if (!$force && $real->lessThan(new Number(0))) { throw new ValueNotConvertibleException($real); } if (null === $roundingMode) { $roundingMode = RoundingMode::HALF_UP(); } $naturalValue = abs(Integer::fromReal($real, $roundingMode)->value()); return new static($naturalValue); }
/** * Multiply the Money amount for a given number and returns a new Money object. * Use 0 < Real $multipler < 1 for division. * * @param Real $multiplier * @param mixed $rounding_mode Rounding mode of the operation. Defaults to RoundingMode::HALF_UP. * @return Money */ public function multiply(Real $multiplier, RoundingMode $rounding_mode = null) { if (null === $rounding_mode) { $rounding_mode = RoundingMode::HALF_UP(); } $amount = $this->getAmount()->toNative() * $multiplier->toNative(); $roundedAmount = new Integer(round($amount, 0, $rounding_mode->toNative())); $result = new self($roundedAmount, $this->getCurrency()); return $result; }
public function testNegativeToNaturalHalfUp() { $real = new Real(-0.5); $natural = $real->toNatural(RoundingMode::HALF_UP()); $this->assertInstanceOf(Natural::class, $natural); $this->assertSame(1, $natural->value()); }