コード例 #1
0
 public static function fromReal(Real $real, RoundingMode $roundingMode = null, $force = true)
 {
     if (!$force && $real->lessThan(new Number(1))) {
         throw new ValueNotConvertibleException($real);
     }
     return parent::fromReal($real, $roundingMode, $force);
 }
コード例 #2
0
ファイル: Integer.php プロジェクト: valueobjects/number
 /**
  * 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);
 }
コード例 #3
0
 /**
  * Returns a Complex given polar coordinates
  *
  * @param  Real    $modulus
  * @param  Real    $argument
  * @return Complex
  */
 public static function fromPolar(Real $modulus, Real $argument)
 {
     $realValue = $modulus->toNative() * \cos($argument->toNative());
     $imValue = $modulus->toNative() * \sin($argument->toNative());
     $real = new Real($realValue);
     $im = new Real($imValue);
     $complex = new static($real, $im);
     return $complex;
 }
コード例 #4
0
ファイル: Natural.php プロジェクト: valueobjects/number
 /**
  * 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);
 }
コード例 #5
0
ファイル: Integer.php プロジェクト: cermat/valueobjects
 /**
  * Returns the value of the integer number
  *
  * @return int
  */
 public function toNative()
 {
     $value = parent::toNative();
     return \intval($value);
 }
コード例 #6
0
ファイル: ComplexTest.php プロジェクト: cermat/valueobjects
 public function testGetArgument()
 {
     $arg = new Real(1.0010398733119);
     $this->assertTrue($arg->sameValueAs($this->complex->getArgument()));
 }
コード例 #7
0
 /**
  * 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;
 }
コード例 #8
0
ファイル: RealTest.php プロジェクト: valueobjects/number
 public function testNegativeToNaturalHalfUp()
 {
     $real = new Real(-0.5);
     $natural = $real->toNatural(RoundingMode::HALF_UP());
     $this->assertInstanceOf(Natural::class, $natural);
     $this->assertSame(1, $natural->value());
 }
コード例 #9
0
ファイル: RealTest.php プロジェクト: cermat/valueobjects
 public function testToString()
 {
     $real = new Real(0.7);
     $this->assertEquals('.7', $real->__toString());
 }