fromInteger() public static method

public static fromInteger ( integer $intValue ) : Decimal
$intValue integer
return Decimal
 public function testNegativeNumbers()
 {
     $this->assertFalse(Decimal::fromInteger(-1)->isZero());
     $this->assertFalse(Decimal::fromFloat(-1.0)->isZero());
     $this->assertFalse(Decimal::fromFloat(-0.1)->isZero());
     $this->assertFalse(Decimal::fromString('-1')->isZero());
 }
 public function testIntegerRound()
 {
     $this->assertTrue(Decimal::fromFloat(0.4)->round()->isZero());
     $this->assertTrue(Decimal::fromFloat(0.4)->round()->equals(Decimal::fromInteger(0)));
     $this->assertFalse(Decimal::fromFloat(0.5)->round()->isZero());
     $this->assertTrue(Decimal::fromFloat(0.5)->round()->equals(Decimal::fromInteger(1)));
 }
 public function testIntegers()
 {
     $this->assertTrue(Decimal::fromInteger(-200)->isInteger());
     $this->assertTrue(Decimal::fromInteger(-2)->isInteger());
     $this->assertTrue(Decimal::fromInteger(-1)->isInteger());
     $this->assertTrue(Decimal::fromInteger(0)->isInteger());
     $this->assertTrue(Decimal::fromInteger(1)->isInteger());
     $this->assertTrue(Decimal::fromInteger(2)->isInteger());
     $this->assertTrue(Decimal::fromInteger(200)->isInteger());
     $this->assertTrue(Decimal::fromString("-200")->isInteger());
     $this->assertTrue(Decimal::fromString("-2")->isInteger());
     $this->assertTrue(Decimal::fromString("-1")->isInteger());
     $this->assertTrue(Decimal::fromString("0")->isInteger());
     $this->assertTrue(Decimal::fromString("1")->isInteger());
     $this->assertTrue(Decimal::fromString("2")->isInteger());
     $this->assertTrue(Decimal::fromString("200")->isInteger());
     $this->assertTrue(Decimal::fromString("-200.000")->isInteger());
     $this->assertTrue(Decimal::fromString("-2.000")->isInteger());
     $this->assertTrue(Decimal::fromString("-1.000")->isInteger());
     $this->assertTrue(Decimal::fromString("0.000")->isInteger());
     $this->assertTrue(Decimal::fromString("1.000")->isInteger());
     $this->assertTrue(Decimal::fromString("2.000")->isInteger());
     $this->assertTrue(Decimal::fromString("200.000")->isInteger());
     $this->assertTrue(Decimal::fromFloat(-200.0)->isInteger());
     $this->assertTrue(Decimal::fromFloat(-2.0)->isInteger());
     $this->assertTrue(Decimal::fromFloat(-1.0)->isInteger());
     $this->assertTrue(Decimal::fromFloat(0.0)->isInteger());
     $this->assertTrue(Decimal::fromFloat(1.0)->isInteger());
     $this->assertTrue(Decimal::fromFloat(2.0)->isInteger());
     $this->assertTrue(Decimal::fromFloat(200.0)->isInteger());
 }
Exemplo n.º 4
0
 public function testZeroAdd()
 {
     $z = Decimal::fromInteger(0);
     $n = Decimal::fromInteger(5);
     $this->assertTrue($z->add($n)->equals($n));
     $this->assertTrue($n->add($z)->equals($n));
 }
 public function testFiniteAbs()
 {
     $this->assertTrue(DecimalConstants::pi()->equals(Decimal::fromString("3.14159265358979323846264338327950")));
     $this->assertTrue(DecimalConstants::eulerMascheroni()->equals(Decimal::fromString("0.57721566490153286060651209008240")));
     $this->assertTrue(DecimalConstants::goldenRatio()->equals(Decimal::fromString("1.61803398874989484820458683436564")));
     $this->assertTrue(DecimalConstants::lightSpeed()->equals(Decimal::fromInteger(299792458)));
 }
 public function testBasicCases()
 {
     $one = Decimal::fromInteger(1);
     $ten = Decimal::fromInteger(10);
     $this->assertTrue($one->comp($ten) === -1);
     $this->assertTrue($ten->comp($one) === 1);
 }
 public function testSimpleNotEquals()
 {
     // Symmetry
     $this->assertFalse(Decimal::fromInteger(1)->equals(Decimal::fromInteger(2)));
     $this->assertFalse(Decimal::fromInteger(2)->equals(Decimal::fromInteger(1)));
     $this->assertFalse(Decimal::fromFloat(1.01)->equals(Decimal::fromInteger(1)));
     $this->assertFalse(Decimal::fromInteger(1)->equals(Decimal::fromFloat(1.01)));
 }
Exemplo n.º 8
0
 public function toFileMakerValue($value)
 {
     if (null === $value) {
         return null;
     }
     Assertion::integer($value);
     return Decimal::fromInteger($value);
 }
 public function testNegativeInfiniteNegativePower()
 {
     $nInf = InfiniteDecimal::getNegativeInfinite();
     $zero = DecimalConstants::Zero();
     $this->assertTrue($nInf->pow(Decimal::fromInteger(-1))->equals($zero));
     $this->assertTrue($nInf->pow(Decimal::fromInteger(-2))->equals($zero));
     $this->assertTrue($nInf->pow(Decimal::fromInteger(-3))->equals($zero));
     $this->assertTrue($nInf->pow($nInf)->equals($zero));
 }
 public function testFiniteInfiniteComp()
 {
     $ten = Decimal::fromInteger(10);
     $pInf = Decimal::getPositiveInfinite();
     $nInf = Decimal::getNegativeInfinite();
     $this->assertTrue($ten->comp($pInf) === -1);
     $this->assertTrue($ten->comp($nInf) === 1);
     $this->assertTrue($pInf->comp($ten) === 1);
     $this->assertTrue($nInf->comp($ten) === -1);
 }
 public function testAsFloat()
 {
     $this->assertEquals(1.0, Decimal::fromString('1.0')->asFloat());
     $this->assertTrue(is_float(Decimal::fromString('1.0')->asFloat()));
     $this->assertEquals(1.0, Decimal::fromInteger(1)->asFloat());
     $this->assertTrue(is_float(Decimal::fromInteger(1)->asFloat()));
     $this->assertEquals(1.0, Decimal::fromFloat(1.0)->asFloat());
     $this->assertEquals(1.123123123, Decimal::fromString('1.123123123')->asFloat());
     $this->assertTrue(is_float(Decimal::fromFloat(1.0)->asFloat()));
     $this->assertTrue(is_float(Decimal::fromString('1.123123123')->asFloat()));
 }
Exemplo n.º 12
0
 public function testIntegerFloor()
 {
     $this->assertTrue(Decimal::fromFloat(0.0)->floor()->isZero());
     $this->assertTrue(Decimal::fromFloat(0.0)->floor()->equals(Decimal::fromInteger(0)));
     $this->assertTrue(Decimal::fromFloat(0.01)->floor()->isZero());
     $this->assertTrue(Decimal::fromFloat(0.4)->floor()->isZero());
     $this->assertTrue(Decimal::fromFloat(0.5)->floor()->isZero());
     $this->assertTrue(Decimal::fromFloat(0.01)->floor()->equals(Decimal::fromInteger(0)));
     $this->assertTrue(Decimal::fromFloat(0.4)->floor()->equals(Decimal::fromInteger(0)));
     $this->assertTrue(Decimal::fromFloat(0.5)->floor()->equals(Decimal::fromInteger(0)));
     $this->assertTrue(Decimal::fromFloat(1.01)->floor()->equals(Decimal::fromInteger(1)));
     $this->assertTrue(Decimal::fromFloat(1.4)->floor()->equals(Decimal::fromInteger(1)));
     $this->assertTrue(Decimal::fromFloat(1.5)->floor()->equals(Decimal::fromInteger(1)));
 }
Exemplo n.º 13
0
 public function testFiniteInfiniteSub()
 {
     $pInf = Decimal::getPositiveInfinite();
     $nInf = Decimal::getNegativeInfinite();
     $pTen = Decimal::fromInteger(10);
     $nTen = Decimal::fromInteger(-10);
     $this->assertTrue($pInf->sub($pTen)->equals($pInf));
     $this->assertTrue($nInf->sub($pTen)->equals($nInf));
     $this->assertTrue($pInf->sub($nTen)->equals($pInf));
     $this->assertTrue($nInf->sub($nTen)->equals($nInf));
     $this->assertTrue($pTen->sub($pInf)->equals($nInf));
     $this->assertTrue($pTen->sub($nInf)->equals($pInf));
     $this->assertTrue($nTen->sub($pInf)->equals($nInf));
     $this->assertTrue($nTen->sub($nInf)->equals($pInf));
 }
Exemplo n.º 14
0
 public function testBasicDiv()
 {
     $one = Decimal::fromInteger(1);
     $two = Decimal::fromInteger(2);
     $four = Decimal::fromInteger(4);
     $eight = Decimal::fromInteger(8);
     // Integer exact division
     $this->assertTrue($eight->div($two)->equals($four));
     $this->assertTrue($eight->div($four)->equals($two));
     // Arbitrary precision division
     $this->assertTrue($one->div($eight, 0)->equals(Decimal::fromString('0')));
     $this->assertTrue($one->div($eight, 1)->equals(Decimal::fromString('0.1')));
     $this->assertTrue($one->div($eight, 2)->equals(Decimal::fromString('0.13')));
     $this->assertTrue($one->div($eight, 3)->equals(Decimal::fromString('0.125')));
 }
Exemplo n.º 15
0
 /**
  * Parse condition divided to field, operator and value
  *
  * @param string field
  * @return array
  */
 protected function _parseCondition($field, $operator, $value)
 {
     // sum function
     $sum = function ($item) use($field) {
         $sum = Decimal::fromInteger(0);
         foreach ($item[$field] as $s) {
             $sum = $sum->add($s);
         }
         return $sum;
     };
     // convert value to decimal
     $value = Decimal::fromFloat($this->_parseDecimal($value));
     switch ($operator) {
         case '=':
             return function ($item) use($sum, $value) {
                 $total = $sum($item);
                 return $total->equals($value);
             };
         case 'NOT':
             return function ($item) use($sum, $value) {
                 $total = $sum($item);
                 return $total->equals($value);
             };
         case '>=':
             return function ($item) use($sum, $value) {
                 $total = $sum($item);
                 return $total->comp($value) > -1;
             };
         case '<=':
             return function ($item) use($sum, $value) {
                 $total = $sum($item);
                 return $total->comp($value) < 1;
             };
         case '>':
             return function ($item) use($sum, $value) {
                 $total = $sum($item);
                 return $total->comp($value) > 0;
             };
         case '<':
             return function ($item) use($sum, $value) {
                 $total = $sum($item);
                 return $total->comp($value) < 0;
             };
     }
     return [$field . $operator => $value];
 }
Exemplo n.º 16
0
 public function testInfiniteMul()
 {
     $pInf = Decimal::getPositiveInfinite();
     $nInf = Decimal::getNegativeInfinite();
     $pOne = Decimal::fromInteger(1);
     $nOne = Decimal::fromInteger(-1);
     $oipp = $pOne->mul($pInf);
     $oipn = $pOne->mul($nInf);
     $oinp = $nOne->mul($pInf);
     $oinn = $nOne->mul($nInf);
     $this->assertTrue($oipp->isPositive());
     $this->assertTrue($oinn->isPositive());
     $this->assertTrue($oinp->isNegative());
     $this->assertTrue($oipn->isNegative());
     $this->assertTrue($oipp->isInfinite());
     $this->assertTrue($oipn->isInfinite());
     $this->assertTrue($oinp->isInfinite());
     $this->assertTrue($oinn->isInfinite());
 }
 public function testExponentialNotationString_With_PositiveExponent_And_NegativeSign()
 {
     $this->assertTrue(Decimal::fromString('-1e3')->equals(Decimal::fromInteger(-1000)));
     $this->assertTrue(Decimal::fromString('-1.5e3')->equals(Decimal::fromInteger(-1500)));
 }
Exemplo n.º 18
0
 /**
  * Calculate totals
  *
  * @param callable filter
  * @return array
  */
 protected function _calculateTotals($filter)
 {
     if (!is_callable($filter)) {
         throw new \InvalidArgumentException('Filter for _calculateTotals method has to be callable.');
     }
     $taxTotals = [];
     $weight = Decimal::fromInteger(0);
     foreach ($this->getItems($filter) as $item) {
         $price = $this->getItemPrice($item);
         if (!isset($taxTotals[$item->getTaxRate()])) {
             $taxTotals[$item->getTaxRate()] = Decimal::fromInteger(0);
         }
         $taxTotals[$item->getTaxRate()] = $taxTotals[$item->getTaxRate()]->add($price);
         // weight
         if ($item instanceof WeightedCartItemInterface) {
             $itemWeight = Decimal::fromFloat((double) $item->getWeight());
             $itemWeight = $itemWeight->mul(Decimal::fromInteger((int) $item->getCartQuantity()));
             $weight = $weight->add($itemWeight);
         }
     }
     $totals = ['subtotals' => [], 'taxes' => [], 'totals' => [], 'weight' => $weight->round(6)];
     foreach ($taxTotals as $taxRate => $amount) {
         if ($this->_pricesWithVat) {
             $totals['totals'][$taxRate] = $amount;
             $totals['taxes'][$taxRate] = $amount->mul(Decimal::fromFloat(1 - 1 / (1 + (double) $taxRate / 100)))->round($this->_roundingDecimals);
             $totals['subtotals'][$taxRate] = $amount->sub($totals['taxes'][$taxRate]);
         } else {
             $totals['subtotals'][$taxRate] = $amount;
             $totals['taxes'][$taxRate] = $amount->mul(Decimal::fromFloat((double) $taxRate / 100))->round($this->_roundingDecimals);
             $totals['totals'][$taxRate] = $amount->add($totals['taxes'][$taxRate]);
         }
     }
     return $totals;
 }
Exemplo n.º 19
0
 /**
  * Internal method used to compute arctan and arccotan
  *
  * @param Decimal $x
  * @param Decimal $firstTerm
  * @param $scale
  * @return Decimal
  */
 private static function simplePowerSerie(Decimal $x, Decimal $firstTerm, $scale)
 {
     $approx = $firstTerm;
     $change = InfiniteDecimal::getPositiveInfinite();
     $xPowerN = DecimalConstants::One();
     // Calculates x^n
     $sign = DecimalConstants::One();
     // Calculates a_n
     for ($i = 1; !$change->floor($scale + 2)->isZero(); $i++) {
         $xPowerN = $xPowerN->mul($x);
         if ($i % 2 === 0) {
             $factorN = DecimalConstants::zero();
         } else {
             if ($i % 4 === 1) {
                 $factorN = DecimalConstants::one()->div(Decimal::fromInteger($i), $scale + 2);
             } else {
                 $factorN = DecimalConstants::negativeOne()->div(Decimal::fromInteger($i), $scale + 2);
             }
         }
         if (!$factorN->isZero()) {
             $change = $factorN->mul($xPowerN, $scale + 2);
             $approx = $approx->add($change, $scale + 2);
         }
     }
     return $approx->round($scale);
 }
Exemplo n.º 20
0
 /**
  * @expectedException \DomainException
  * @expectedExceptionMessage The tangent of this 'angle' is undefined.
  */
 public function testTanPiTwoDiv()
 {
     $PiDividedByTwo = DecimalConstants::PI()->div(Decimal::fromInteger(2));
     $PiDividedByTwo->tan();
 }
Exemplo n.º 21
0
 /**
  * Returns the Light of Speed measured in meters / second.
  * @return Decimal
  */
 public static function lightSpeed()
 {
     if (self::$LightSpeed === null) {
         self::$LightSpeed = Decimal::fromInteger(299792458);
     }
     return self::$LightSpeed;
 }
Exemplo n.º 22
0
 public static function fileMakerBooleanProvider() : array
 {
     return [[null, false], ['', false], ['0', false], ['1', true], ['test', true], [Decimal::fromInteger(0), false], [Decimal::fromInteger(1), true], [Decimal::fromInteger(2), true], [Decimal::fromInteger(-1), true]];
 }
 public static function validXmlProvider() : array
 {
     return ['project-sample-data' => ['projectsampledata.xml', [['record-id' => 7676, 'mod-id' => 5, 'PROJECT ID MATCH FIELD' => Decimal::fromInteger(1), 'Created By' => 'Tim Thomson', 'Creation TimeStamp' => new DateTimeImmutable('2012-02-22 17:19:47 UTC'), 'Project Name' => 'Launch web site', 'Description' => "Launch the web site with our new branding and product line.\n\n" . "                    Third line", 'Status' => Decimal::fromInteger(4), 'Status on Screen' => 'Overdue', 'Start Date' => new DateTimeImmutable('2011-04-13 00:00:00 UTC'), 'Due Date' => new DateTimeImmutable('2012-05-02 00:00:00 UTC'), 'Days Remaining' => Decimal::fromInteger(0), 'Days Elapsed' => Decimal::fromInteger(275), 'Project Completion' => Decimal::fromString('0.48'), 'Tag' => 'marketing', 'Start Date Project Completion' => new DateTimeImmutable('2011-04-13 00:00:00 UTC'), 'Due Date Project Completion' => new DateTimeImmutable('2012-05-02 00:00:00 UTC'), 'Repeating Field' => ['A1', 'B2', 'C3', 'D4', 'E5', 'F6', 'G7', 'H8', 'I9'], 'Tasks' => [['record-id' => 14999, 'mod-id' => 1, 'Task Name' => 'Site map sketch', 'TASK ID MATCH FIELD' => Decimal::fromInteger(2), 'Repeating Field' => [Decimal::fromInteger(1), Decimal::fromInteger(2), Decimal::fromInteger(3)]], ['record-id' => 15000, 'mod-id' => 1, 'Task Name' => 'Send art to vendor', 'TASK ID MATCH FIELD' => Decimal::fromInteger(4), 'Repeating Field' => [Decimal::fromInteger(4), Decimal::fromInteger(5), Decimal::fromInteger(6)]], ['record-id' => 15001, 'mod-id' => 1, 'Task Name' => 'Review mock ups', 'TASK ID MATCH FIELD' => Decimal::fromInteger(5), 'Repeating Field' => [Decimal::fromInteger(7), Decimal::fromInteger(8), Decimal::fromInteger(9)]], ['record-id' => 15002, 'mod-id' => 0, 'Task Name' => 'Write page text', 'TASK ID MATCH FIELD' => Decimal::fromInteger(6), 'Repeating Field' => [null, null, null]], ['record-id' => 15003, 'mod-id' => 0, 'Task Name' => 'New logo art', 'TASK ID MATCH FIELD' => Decimal::fromInteger(3), 'Repeating Field' => [null, null, null]]]], ['record-id' => 7677, 'mod-id' => 4, 'PROJECT ID MATCH FIELD' => Decimal::fromInteger(7), 'Created By' => 'Tim Thomson', 'Creation TimeStamp' => new DateTimeImmutable('2012-02-22 17:19:47 UTC'), 'Project Name' => 'Prototype', 'Description' => "Build a working prototype of the new product.\n\n\n" . "                    Fourth line.", 'Status' => Decimal::fromInteger(4), 'Status on Screen' => 'Overdue', 'Start Date' => new DateTimeImmutable('2012-02-06 00:00:00 UTC'), 'Due Date' => new DateTimeImmutable('2012-02-17 00:00:00 UTC'), 'Days Remaining' => Decimal::fromInteger(0), 'Days Elapsed' => Decimal::fromInteger(9), 'Project Completion' => Decimal::fromString('0.32'), 'Tag' => 'manufacturing', 'Start Date Project Completion' => new DateTimeImmutable('2012-02-09 00:00:00 UTC'), 'Due Date Project Completion' => new DateTimeImmutable('2012-02-17 00:00:00 UTC'), 'Repeating Field' => ['*1', '-2', '+3', '.4', '/5', '=6', '=7', '-8', '`9'], 'Tasks' => [['record-id' => 15005, 'mod-id' => 1, 'Task Name' => 'Build prototype', 'TASK ID MATCH FIELD' => Decimal::fromInteger(9), 'Repeating Field' => [null, null, null]], ['record-id' => 15006, 'mod-id' => 1, 'Task Name' => 'Review sketches', 'TASK ID MATCH FIELD' => Decimal::fromInteger(8), 'Repeating Field' => [null, null, null]], ['record-id' => 15007, 'mod-id' => 1, 'Task Name' => 'Complete sketches', 'TASK ID MATCH FIELD' => Decimal::fromInteger(7), 'Repeating Field' => [null, null, null]], ['record-id' => 15014, 'mod-id' => 0, 'Task Name' => 'Draft requirements', 'TASK ID MATCH FIELD' => Decimal::fromInteger(16), 'Repeating Field' => [null, null, null]], ['record-id' => 15015, 'mod-id' => 0, 'Task Name' => 'Review requirements', 'TASK ID MATCH FIELD' => Decimal::fromInteger(17), 'Repeating Field' => [null, null, null]]]], ['record-id' => 7678, 'mod-id' => 4, 'PROJECT ID MATCH FIELD' => Decimal::fromInteger(13), 'Created By' => 'Tim Thomson', 'Creation TimeStamp' => new DateTimeImmutable('2012-02-22 17:19:47 UTC'), 'Project Name' => 'Investor meeting', 'Description' => "This is important. We need the investors to have confidence.\n" . "                    Second line.", 'Status' => Decimal::fromInteger(4), 'Status on Screen' => 'Overdue', 'Start Date' => new DateTimeImmutable('2011-12-12 00:00:00 UTC'), 'Due Date' => new DateTimeImmutable('2012-03-22 00:00:00 UTC'), 'Days Remaining' => Decimal::fromInteger(0), 'Days Elapsed' => Decimal::fromInteger(73), 'Project Completion' => Decimal::fromString('0.4285714285714286'), 'Tag' => 'finance', 'Start Date Project Completion' => new DateTimeImmutable('2012-01-02 00:00:00 UTC'), 'Due Date Project Completion' => new DateTimeImmutable('2012-03-22 00:00:00 UTC'), 'Repeating Field' => ['a1', 'b2', 'c3', 'd4', 'e5', 'f6', 'g7', 'h8', 'i9'], 'Tasks' => [['record-id' => 15004, 'mod-id' => 1, 'Task Name' => 'Gather requirements', 'TASK ID MATCH FIELD' => Decimal::fromInteger(1), 'Repeating Field' => [null, Decimal::fromInteger(0), null]], ['record-id' => 15008, 'mod-id' => 1, 'Task Name' => 'Investor meeting', 'TASK ID MATCH FIELD' => Decimal::fromInteger(13), 'Repeating Field' => [null, null, Decimal::fromInteger(0)]], ['record-id' => 15009, 'mod-id' => 1, 'Task Name' => 'Final draft of slides', 'TASK ID MATCH FIELD' => Decimal::fromInteger(12), 'Repeating Field' => [null, null, null]], ['record-id' => 15010, 'mod-id' => 0, 'Task Name' => 'Complete business plan', 'TASK ID MATCH FIELD' => Decimal::fromInteger(10), 'Repeating Field' => [null, null, null]], ['record-id' => 15011, 'mod-id' => 0, 'Task Name' => 'First draft of slides', 'TASK ID MATCH FIELD' => Decimal::fromInteger(11), 'Repeating Field' => [null, null, null]], ['record-id' => 15012, 'mod-id' => 0, 'Task Name' => 'Market research', 'TASK ID MATCH FIELD' => Decimal::fromInteger(14), 'Repeating Field' => [null, null, null]], ['record-id' => 15013, 'mod-id' => 0, 'Task Name' => 'Competitive analysis', 'TASK ID MATCH FIELD' => Decimal::fromInteger(15), 'Repeating Field' => [null, null, null]]]]], 3], 'empty-resultset' => ['sample_fmresultset_empty.xml', [], 0]];
 }
Exemplo n.º 24
0
 public function toFileMakerValue($value)
 {
     Assertion::boolean($value);
     return Decimal::fromInteger($value ? 1 : 0);
 }
Exemplo n.º 25
0
 protected function applyValue($input, Context $ctx)
 {
     $output = $input;
     cast:
     $allowString = $this->allowString === null ? true : $this->allowString;
     try {
         $cast = $input;
         if ($allowString && is_string($input)) {
             if ($input === '') {
                 $cast = null;
             } else {
                 $cast = BigNumbers\Decimal::fromString($input);
             }
         } elseif ($this->allowInt && is_int($input)) {
             $cast = BigNumbers\Decimal::fromInteger($input);
         } elseif ($this->allowDouble && is_float($input)) {
             $cast = BigNumbers\Decimal::fromFloat($input);
         }
         if ($cast !== $input) {
             $ctx->setChange(Change::Internal);
         }
         $output = $cast;
     } catch (\Exception $ex) {
         $ctx->addReason($this, ['id' => 'decimal.invalid']);
         goto done;
     }
     // must cast before this so we can cast to null
     if ($output === null) {
         goto done;
     }
     type:
     if (!$output instanceof BigNumbers\Decimal) {
         $ctx->addReason($this, ['id' => 'decimal.invalid']);
         goto done;
     }
     scale:
     if ($this->scale !== null) {
         if (preg_match('/\\.([0-9]+)$/', $output . '', $match)) {
             $inScale = strlen($match[1]);
             if ($inScale > $this->scale) {
                 $ctx->addReason($this, ['id' => 'decimal.scale', 'params' => ['scale' => $inScale, 'expected' => $this->scale]]);
             }
         }
     }
     precision:
     if ($this->precision !== null) {
         $digits = preg_replace("/[^0-9]/", '', $output . '');
         $inPrecision = strlen($digits);
         if ($inPrecision > $this->precision) {
             $ctx->addReason($this, ['id' => 'decimal.precision', 'params' => ['precision' => $inPrecision, 'expected' => $this->precision]]);
         }
     }
     minmax:
     $min = $this->min !== null ? BigNumbers\Decimal::create($this->min) : null;
     $max = $this->max !== null ? BigNumbers\Decimal::create($this->max) : null;
     if ($min !== null && $max !== null) {
         if ($output->comp($min) < 0 || $output->comp($max) > 0) {
             $ctx->addReason($this, ['id' => 'decimal.between', 'params' => ['atLeast' => $min . '', 'atMost' => $max . '']]);
         }
     } elseif ($min !== null) {
         if ($output->comp($min) < 0) {
             $ctx->addReason($this, ['id' => 'decimal.atLeast', 'params' => ['atLeast' => $min . '']]);
         }
     } elseif ($max !== null) {
         if ($output->comp($max) > 0) {
             $ctx->addReason($this, ['id' => 'decimal.atMost', 'params' => ['atMost' => $max . '']]);
         }
     }
     divisibleBy:
     if ($this->divisibleBy !== null) {
         $divisibleBy = !$this->divisibleBy instanceof BigNumbers\Decimal ? BigNumbers\Decimal::create($this->divisibleBy) : $this->divisibleBy;
         if (!$output->mod($divisibleBy)->isZero()) {
             $dvFmt = $this->removeTrailingZeroes($divisibleBy);
             $ctx->addReason($this, ['id' => 'decimal.divisibleBy', 'params' => ['divisibleBy' => $dvFmt]]);
         }
     }
     done:
     if ($this->emitString && $output instanceof BigNumbers\Decimal) {
         $output = $output->innerValue();
         if (!is_string($input)) {
             $ctx->setChange(Change::Internal);
         }
     }
     return $output;
 }
 public function testPositiveAdditiveInverse()
 {
     $this->assertTrue(Decimal::fromInteger(1)->additiveInverse()->equals(Decimal::fromInteger(-1)));
     $this->assertTrue(Decimal::fromString('1.768')->additiveInverse()->equals(Decimal::fromString('-1.768')));
 }
Exemplo n.º 27
0
 public function testSuccessfulConversionToFileMaker()
 {
     $type = new DecimalType();
     $value = Decimal::fromInteger(1);
     $this->assertSame($value, $type->toFileMakerValue($value));
 }
Exemplo n.º 28
0
 public function testNegativeWithNegativeExponent()
 {
     $nFive = Decimal::fromInteger(-5);
     $this->assertTrue($nFive->pow(Decimal::fromInteger(-1))->equals(Decimal::fromString("-0.2")), "The answer must be -0.2, but was " . $nFive->pow(Decimal::fromInteger(-1)));
     $this->assertTrue($nFive->pow(Decimal::fromInteger(-2))->equals(Decimal::fromString("0.04")));
     $this->assertTrue($nFive->pow(Decimal::fromInteger(-3))->equals(Decimal::fromString("-0.008")));
 }
 public function testCreateFromInteger()
 {
     $this->assertTrue(Decimal::create(-35)->equals(Decimal::fromInteger(-35)));
     $this->assertTrue(Decimal::create(0)->equals(Decimal::fromInteger(0)));
     $this->assertTrue(Decimal::create(35)->equals(Decimal::fromInteger(35)));
 }
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage $scale must be a positive integer
  */
 public function testOperatorNegativeScaleValidation()
 {
     $one = Decimal::fromInteger(1);
     $one->mul($one, -1);
 }