/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $number = $input->getArgument(self::NUMBER_ARGUMENT); $number = $this->filter->filter($number); $decimalFormat = $this->factory->make($number); $scientificNotation = $this->transformer->transform($decimalFormat); $output->writeLn((string) $scientificNotation); }
/** * Return new ScientificNotation entity * * @param string $notation * @return ScientificNotation */ public function make($notation) { if (!preg_match(self::SCIENTIFIC_NOTATION_PATTERN, $notation, $matches)) { throw new InvalidScientificNotationException($notation); } $coefficient = $this->numberFactory->make($matches['coefficient']); $exponent = $this->integerFactory->make($matches['exponent']); return new ScientificNotation($coefficient, $exponent); }
/** * Transform notation entity into numeric * * @param NotationInterface $notation * @return Entity\Number */ public function transform(NotationInterface $notation) { $exponent = $notation->getExponent(); $coefficient = $notation->getCoefficient(); $integer = $coefficient->getInteger(); $padLength = strlen($integer->getValue()) + abs($exponent->getValue()); $result = str_pad($integer->getValue(), $padLength, '0', $exponent->isNegative() ? STR_PAD_LEFT : STR_PAD_RIGHT); if ($exponent->isNegative()) { $result = sprintf('%s%s', $result, $coefficient->getFractional()); $result = substr_replace($result, '.', 1, 0); } else { $result = substr_replace($result, $coefficient->getFractional(), strlen($integer->getValue()), strlen($coefficient->getFractional())); if ($coefficient->isFloat()) { $result = substr_replace($result, '.', (int) $exponent->getValue() + 1, 0); } } $result = sprintf('%s%s', $coefficient->getInteger()->getSign(), $result); return $this->numberFactory->make($result); }