Example #1
0
 /**
  * Creates a BigRational of the given value.
  *
  * @param BigNumber|number|string $value
  *
  * @return BigRational
  *
  * @throws ArithmeticException If the value cannot be converted to a BigRational.
  */
 public static function of($value)
 {
     return parent::of($value)->toBigRational();
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function applyTo(BigNumber $amount, Currency $currency, $currentScale)
 {
     return $amount->toScale($currency->getDefaultFractionDigits(), $this->roundingMode);
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function compareTo($that)
 {
     $that = BigNumber::of($that);
     if ($that instanceof BigInteger) {
         return Calculator::get()->cmp($this->value, $that->value);
     }
     return -$that->compareTo($this);
 }
Example #4
0
 /**
  * Returns a Money of the given amount and currency.
  *
  * By default, the amount is scaled to match the currency's default fraction digits.
  * For example, `Money::of('2.5', 'USD')` will yield `USD 2.50`.
  * If amount cannot be safely converted to this scale, an exception is thrown.
  *
  * This behaviour can be overridden by providing the `$fractionDigits` and `$roundingMode` parameters.
  *
  * @param BigNumber|number|string  $amount         The monetary amount.
  * @param Currency|string          $currency       The currency, as a `Currency` object or currency code string.
  * @param int|null                 $fractionDigits The number of fraction digits, or null to use the default.
  * @param int                      $roundingMode   The rounding mode to use, if necessary.
  *
  * @return Money
  *
  * @throws NumberFormatException      If the amount is a string in a non-supported format.
  * @throws RoundingNecessaryException If the rounding was necessary to represent the amount at the requested scale.
  */
 public static function of($amount, $currency, $fractionDigits = null, $roundingMode = RoundingMode::UNNECESSARY)
 {
     $currency = Currency::of($currency);
     if ($fractionDigits === null) {
         $fractionDigits = $currency->getDefaultFractionDigits();
     }
     $amount = BigNumber::of($amount)->toScale($fractionDigits, $roundingMode);
     return new Money($amount, $currency);
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 public function compareTo($that)
 {
     $that = BigNumber::of($that);
     if ($that instanceof BigInteger) {
         $that = $that->toBigDecimal();
     }
     if ($that instanceof BigDecimal) {
         $this->scaleValues($this, $that, $a, $b);
         return Calculator::get()->cmp($a, $b);
     }
     return -$that->compareTo($this);
 }
Example #6
0
 /**
  * {@inheritdoc}
  */
 public function applyTo(BigNumber $amount, Currency $currency, $currentScale)
 {
     return $amount->toScale($this->scale, $this->roundingMode);
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 public function applyTo(BigNumber $amount, Currency $currency, $currentScale)
 {
     return $amount->toBigDecimal();
 }