public function isLowerThan($decimal)
 {
     if (!$decimal instanceof Decimal) {
         $decimal = new Decimal($decimal);
     }
     return bccomp($this->amount, $decimal->getAmount(), self::$scale) === -1;
 }
 public function testSerial()
 {
     $decimal = new Decimal(10);
     $decimal2 = $decimal->fromSerial($decimal->toSerial());
     $this->assertEquals(1000, $decimal2->power(3));
     $this->assertEquals(10, $decimal->get());
 }
 /**
  * @param Decimal $value
  * @return int
  */
 private function getMaxScale(Decimal $value)
 {
     $scale = $this->getScale();
     if ($value->getScale() > $scale) {
         $scale = $value->getScale();
     }
     return $scale;
 }
Exemple #4
0
 /**
  * Create a new Decimal field.
  */
 function __construct($name, $precision = 4)
 {
     if (!$precision) {
         $precision = 4;
     }
     parent::__construct($name, $precision, $precision);
 }
 public function prepValueForDB($value)
 {
     $value = parent::prepValueForDB($value);
     $ciphertext = $this->service->encrypt($value);
     $this->value = $ciphertext;
     return $ciphertext;
 }
 public function saveInto($dataObject)
 {
     parent::saveInto($dataObject);
     $fieldName = $this->name;
     if ($fieldName && $dataObject->{$fieldName} > 1.0) {
         $dataObject->{$fieldName} = 1.0;
     }
 }
Exemple #7
0
 public function getType($object)
 {
     $type = \strtolower(\gettype($object));
     if (is_int($type)) {
         $int = Integer::parse($object);
         //If an integer type was not enough to store the value
         //let it fall through to string type
         if ($int !== null) {
             return $int;
         }
     }
     if (\is_float($object)) {
         //Prefer decimal over floating point
         return Decimal::parse($object);
     }
     if (\is_bool($object)) {
         return Boolean::parse($object);
     }
     return String::parse($object);
 }
Exemple #8
0
 /**
  * {@inheritdoc}
  */
 public function toDecimal()
 {
     return Decimal::fromNative($this->value);
 }
Exemple #9
0
 public function testValidatePassNull()
 {
     $this->meta->null = true;
     $this->assertTrue($this->object->validate($this->meta, null));
 }
 public function __construct($name = null, $wholeSize = 9, $decimalSize = 2, $defaultValue = 0)
 {
     parent::__construct($name, $wholeSize, $decimalSize, $defaultValue);
 }
Exemple #11
0
 /**
  * maxScale
  *
  * @param Decimal $other
  *
  * @return mixed
  */
 private function maxScale(Decimal $other)
 {
     return max($this->scale, $other->scale());
 }
Exemple #12
0
 /**
  * {@inheritdoc}
  */
 public function powDecimal(Decimal $x, $scale = null)
 {
     return $this->powReal($x->toReal())->toDecimal();
 }
Exemple #13
0
 /**
  * @dataProvider correctRoundings
  * @param string $mode
  * @param string $value
  * @param string $expected
  *
  */
 public function testRoundingWithValidModes($mode, $value, $expected)
 {
     $actual = Decimal::valueOf($value)->round($mode)->__toString();
     $this->assertSame($expected, $actual);
 }
Exemple #14
0
 /**
  * Divides this decimal by another.
  *
  * @param Decimal $other
  * @param int     $scale
  *
  * @return Decimal
  */
 public function divideBy(Decimal $other, int $scale = null) : Decimal
 {
     if ($other->isZero()) {
         throw new \LogicException('Cannot divide by zero.');
     }
     if ($other->isOne()) {
         return $this;
     }
     if (null === $scale) {
         // Determine a reasonable scale to avoid too much loss of precision.
         $abs = $this->abs();
         $otherAbs = $other->abs();
         $otherLog10 = self::computeLog10($otherAbs->value, $otherAbs->numberOfDecimalPlaces(), 1);
         $log10 = self::computeLog10($abs->value, $abs->numberOfDecimalPlaces(), 1) - $otherLog10;
         $totalDecimalPlaces = $this->numberOfDecimalPlaces() + $other->numberOfDecimalPlaces();
         $maxSignificantDigits = max($this->numberOfSignificantDigits(), $other->numberOfSignificantDigits());
         $scale = (int) max($totalDecimalPlaces, $maxSignificantDigits - max(ceil($log10), 0), ceil(-$log10) + 1);
     }
     $result = bcdiv($this->value, $other->value, $scale);
     return static::fromString($result);
 }
<?php

FormField::add_extension('BootstrapFormFieldExtension');
Form::add_extension('BootstrapFormExtension');
Decimal::add_extension('ExtendedDecimal');
SS_Datetime::add_extension('ExtendedDatetime');
Exemple #16
0
 /**
  * @param string $columnName
  * @param int $totalDigits
  * @param int $defaultValue
  */
 public function __construct($columnName, $totalDigits = 8, $defaultValue = 0)
 {
     parent::__construct($columnName, $totalDigits, 2, $defaultValue);
 }
Exemple #17
0
 function getValue()
 {
     return (double) parent::getValue();
 }
Exemple #18
0
 protected function isValidValue($value)
 {
     return parent::isValidValue($value) && TypeUtils::isInteger($value);
 }
Exemple #19
0
 /**
  * {@inheritdoc}
  */
 public function multDecimal(Decimal $x, $scale = null)
 {
     if ($x->isInfinite()) {
         return $x->multDecimal($this);
     }
     return new self(\bcmul($this->toNative(), $x->toNative(), $this->scale($scale)));
 }
Exemple #20
0
 /**
  * Raises to the power of the other value.
  *
  * @param Decimal $other
  * @return Decimal
  */
 public function pow(Decimal $other)
 {
     $result = bcpow($this->value, $other->__toString(), self::getScale());
     return self::valueOf($result);
 }
 /**
  * @dataProvider countNonRepeatingData
  */
 function testCountNonRepeating($base, $denominator, $non_repeating)
 {
     $decObj = new Decimal($denominator, 1);
     $this->assertEquals($non_repeating, $decObj->test_count_non_repeating($denominator, $base));
 }
Exemple #22
0
 /**
  * Converts the Ratio to a string.
  *
  * @return string
  */
 public function toString() : string
 {
     return $this->dividend->toString() . ':' . $this->divisor->toString();
 }
Exemple #23
0
 /**
  * @expectedException        RuntimeException
  * @expectedExceptionMessage Not implemented
  */
 public function testSqrt()
 {
     $decimal = new Decimal("4");
     $decimal->sqrt();
 }
Exemple #24
0
 public function __construct($value = 0, $prec = null)
 {
     parent::__construct($value, $prec);
     $this->scale = pow(10, $this->prec);
     $this->value = $this->_normalize($value);
 }