/** * {@inheritdoc} */ public function formatAmount(CurrencyInterface $currency, $amount, $language_type = LanguageInterface::TYPE_CONTENT) { // Compute the number of decimals, so we can format all of them and no less // or more. $decimals = strlen($amount) - strpos($amount, '.') - 1; $currency_locale = $this->localeDelegator->resolveCurrencyLocale(); $formatted_amount = number_format($amount, $decimals, $currency_locale->getDecimalSeparator(), $currency_locale->getGroupingSeparator()); $arguments = array('@currency_code' => $currency->getCurrencyCode(), '@currency_sign' => $currency->getSign(), '@amount' => $formatted_amount); return $this->t('@currency_code @amount', $arguments); }
/** * {@inheritdoc} */ public function formatAmount(CurrencyInterface $currency, $amount, $language_type = LanguageInterface::TYPE_CONTENT) { $currency_locale = $this->localeDelegator->resolveCurrencyLocale(); $decimal_position = strpos($amount, '.'); $number_of_decimals = $decimal_position !== FALSE ? strlen(substr($amount, $decimal_position + 1)) : 0; $formatter = new \NumberFormatter($currency_locale->getLocale(), \NumberFormatter::PATTERN_DECIMAL, $currency_locale->getPattern()); $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $number_of_decimals); $formatter->setSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL, $currency_locale->getDecimalSeparator()); $formatter->setSymbol(\NumberFormatter::MONETARY_SEPARATOR_SYMBOL, $currency_locale->getDecimalSeparator()); $formatter->setSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL, $currency_locale->getGroupingSeparator()); $formatter->setSymbol(\NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL, $currency_locale->getGroupingSeparator()); $formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $currency->getSign()); $formatter->setSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL, $currency->getCurrencyCode()); return $formatted = $formatter->format($amount); }
/** * @covers ::isLessThan * @covers ::comparesTo * @covers ::validateOtherAmount * * @expectedException \InvalidArgumentException */ public function testIsLessThanWithInvalidCurrencies() { $sutCurrencyCode = str_shuffle('FOO'); $otherCurrencyCode = str_shuffle('BAR'); $this->currency->expects($this->atLeastOnce())->method('getCurrencyCode')->willReturn($sutCurrencyCode); $otherCurrency = $this->getMock(CurrencyInterface::class); $otherCurrency->expects($this->atLeastOnce())->method('getCurrencyCode')->willReturn($otherCurrencyCode); $otherAmount = $this->getMock(AmountInterface::class); $otherAmount->expects($this->atLeastOnce())->method('getCurrency')->willReturn($otherCurrency); $this->sut->isLessThan($otherAmount); }