/** * Return number formatter. * * <code> * $fractionDigits = 2; * $numberFormatter = $this->getNumberFormatter($fractionDigits); * </code> * * @param int $fractionDigits * * @return \NumberFormatter */ protected function getNumberFormatter($fractionDigits = 2) { // Prepare decimal pattern. $pattern = '#0'; if ($fractionDigits > 0) { $pattern .= '.' . str_repeat('0', $fractionDigits); } return LocaleHelper::getNumberFormatter($pattern); }
/** * Prepare number formatter. * * <code> * $fractionDigits = 2; * * $this->prepareNumberFormatter($container, $fractionDigits); * </code> * * @param Container $container * @param int $fractionDigits * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \OutOfBoundsException * * @return \NumberFormatter */ protected function prepareNumberFormatter($container, $fractionDigits = 2) { $fractionDigits = (int) abs($fractionDigits); $numberHash = StringHelper::generateMd5Hash(Constants::CONTAINER_FORMATTER_NUMBER, $fractionDigits); if (!$container->exists($numberHash)) { // Prepare decimal pattern. $pattern = '#0'; if ($fractionDigits > 0) { $pattern .= '.' . str_repeat('0', $fractionDigits); } $formatter = LocaleHelper::getNumberFormatter($pattern); $container->set($numberHash, $formatter); } }
/** * Return money formatter. * * <code> * $this->prepareMoneyFormatter($container, $params); * $money = $this->getMoneyFormatter($container, $params); * </code> * * @param Registry $params * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \OutOfBoundsException * * @return Money */ protected function getMoneyFormatter($params) { $currencyId = $params->get('project_currency'); // Get the currency. $currency = $this->getCurrency($currencyId); // Prepare decimal pattern. $fractionDigits = (int) $params->get('fraction_digits', 2); $pattern = '#,##0'; if ($fractionDigits > 0) { $pattern .= '.' . str_repeat('0', $fractionDigits); } $formatter = LocaleHelper::getNumberFormatter($pattern); $money = new Money($formatter); $money->setCurrency($currency); return $money; }
/** * Prepare money formatter. * * <code> * $this->prepareMoneyFormatter($container, $params); * </code> * * @param Container $container * @param Registry $params * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \OutOfBoundsException */ protected function prepareMoneyFormatter($container, $params) { $currencyId = $params->get('project_currency'); $moneyHash = StringHelper::generateMd5Hash(Constants::CONTAINER_FORMATTER_MONEY, $currencyId); if (!$container->exists($moneyHash)) { // Get the currency from the container. $currency = $this->getCurrency($container, $params); // Prepare decimal pattern. $fractionDigits = (int) $params->get('fraction_digits', 2); $pattern = '#,##0'; if ($fractionDigits > 0) { $pattern .= '.' . str_repeat('0', $fractionDigits); } $formatter = LocaleHelper::getNumberFormatter($pattern); $money = new Money($formatter); $money->setCurrency($currency); $container->set($moneyHash, $money); } }