/** * @dataProvider splitDecimalExponentProvider */ public function testSplitDecimalExponent($valueString, $expectedDecimal, $expectedExponent) { $parser = new DecimalParser(); list($decimal, $exponent) = $parser->splitDecimalExponent($valueString); $this->assertSame($expectedDecimal, $decimal); $this->assertSame($expectedExponent, $exponent); }
/** * Constructs a QuantityValue from the given parts. * * @see splitQuantityString * * @param string $amount decimal representation of the amount * @param string|null $exactness either '!' to indicate an exact value, * or '~' for "automatic", or null if $margin should be used. * @param string|null $margin decimal representation of the uncertainty margin * @param string $unit the unit identifier (use "1" for unitless quantities). * * @throws ParseException if one of the decimals could not be parsed. * @throws IllegalValueException if the QuantityValue could not be constructed * @return QuantityValue */ private function newQuantityFromParts($amount, $exactness, $margin, $unit) { list($amount, $exponent) = $this->decimalParser->splitDecimalExponent($amount); $amountValue = $this->decimalParser->parse($amount); if ($exactness === '!') { // the amount is an exact number $amountValue = $this->decimalParser->applyDecimalExponent($amountValue, $exponent); $quantity = $this->newExactQuantity($amountValue, $unit); } elseif ($margin !== null) { // uncertainty margin given // NOTE: the pattern for scientific notation is 2e3 +/- 1e2, so the exponents are treated separately. $marginValue = $this->decimalParser->parse($margin); $amountValue = $this->decimalParser->applyDecimalExponent($amountValue, $exponent); $quantity = $this->newUncertainQuantityFromMargin($amountValue, $unit, $marginValue); } else { // derive uncertainty from given decimals // NOTE: with scientific notation, the exponent applies to the uncertainty bounds, too $quantity = $this->newUncertainQuantityFromDigits($amountValue, $unit, $exponent); } return $quantity; }