/** * @see ValueFormatter::format * * @since 0.1 * * @param DecimalValue $dataValue * * @throws InvalidArgumentException * @return string Text */ public function format($dataValue) { if (!$dataValue instanceof DecimalValue) { throw new InvalidArgumentException('Data value type mismatch. Expected a DecimalValue.'); } // TODO: Implement optional rounding/padding $decimal = $dataValue->getValue(); if (!$this->getOption(self::OPT_FORCE_SIGN)) { // strip leading + $decimal = ltrim($decimal, '+'); } // apply number localization $decimal = $this->localizer->localizeNumber($decimal); return $decimal; }
/** * Compares this DecimalValue to another DecimalValue. * * @since 0.1 * * @param DecimalValue $that * * @throws LogicException * @return int +1 if $this > $that, 0 if $this == $that, -1 if $this < $that */ public function compare(DecimalValue $that) { if ($this === $that) { return 0; } $a = $this->getValue(); $b = $that->getValue(); if ($a === $b) { return 0; } if ($a[0] === '+' && $b[0] === '-') { return 1; } if ($a[0] === '-' && $b[0] === '+') { return -1; } // compare the integer parts $aInt = ltrim($this->getIntegerPart(), '0'); $bInt = ltrim($that->getIntegerPart(), '0'); $sense = $a[0] === '+' ? 1 : -1; // per precondition, there are no leading zeros, so the longer nummber is greater if (strlen($aInt) > strlen($bInt)) { return $sense; } if (strlen($aInt) < strlen($bInt)) { return -$sense; } // if both have equal length, compare alphanumerically $cmp = strcmp($aInt, $bInt); if ($cmp > 0) { return $sense; } if ($cmp < 0) { return -$sense; } // compare fractional parts $aFract = rtrim($this->getFractionalPart(), '0'); $bFract = rtrim($that->getFractionalPart(), '0'); // the fractional part is left-aligned, so just check alphanumeric ordering $cmp = strcmp($aFract, $bFract); return $cmp > 0 ? $sense : ($cmp < 0 ? -$sense : 0); }
/** * Decrement the least significant digit by one if it is more than 0, and * set it to 9 and continue to the next more significant digit if it is 0. * Exception: slump( 0 ) == -1; * * E.g.: slump( 0.2 ) == 0.1, slump( -0.10 ) == -0.01, slump( 0.0 ) == -1.0 * * This is the inverse of @see bump() * * @since 0.1 * * @param DecimalValue $decimal * * @return DecimalValue */ public function slump(DecimalValue $decimal) { $value = $decimal->getValue(); $slumped = $this->slumpDigits($value); return new DecimalValue($slumped); }
/** * @dataProvider computeComplementProvider */ public function testComputeComplement(DecimalValue $value, $expected) { $complement = $value->computeComplement(); $this->assertSame($expected, $complement->getValue()); $actual = $complement->computeComplement(); $this->assertSame($value->getValue(), $actual->getValue()); }
/** * @dataProvider applyDecimalExponentProvider */ public function testApplyDecimalExponent(DecimalValue $decimal, $exponent, DecimalValue $expectedDecimal) { $parser = new DecimalParser(); $actualDecimal = $parser->applyDecimalExponent($decimal, $exponent); $this->assertSame($expectedDecimal->getValue(), $actualDecimal->getValue()); }
public function __toString() { $unit = $this->getUnit(); return $this->amount->getValue() . '[' . $this->lowerBound->getValue() . '..' . $this->upperBound->getValue() . ']' . ($unit === '1' ? '' : $unit); }