Currency symbol provided will be inserted into formatted number string.
Note: currently length is not used in currencyFormats from CLDR.
But it's defined in the specification, so we support it here.
public formatCurrencyNumber ( mixed $number, |
||
$number | mixed | Float or int, can be negative, can be NaN or infinite |
$locale | ||
$currency | string | Currency symbol (or name) |
$formatLength | string | One of NumbersReader FORMAT_LENGTH constants |
Результат | string | Formatted number. Will return string-casted version of $number if there is no pattern for given $locale / $formatLength |
/** * @param string $currencySign (optional) The currency sign, eg $ or €. * @param string $decimalSeparator (optional) The separator for the decimal point. * @param string $thousandsSeparator (optional) The thousands separator. * * @throws InvalidVariableException * @return string the formatted amount. * @throws ViewHelperException * @api */ public function render($currencySign = '', $decimalSeparator = ',', $thousandsSeparator = '.') { $stringToFormat = $this->renderChildren(); $useLocale = $this->getLocale(); if ($useLocale !== null) { if ($currencySign === '') { throw new InvalidVariableException('Using the Locale requires a currencySign.', 1326378320); } try { $output = $this->numberFormatter->formatCurrencyNumber($stringToFormat, $useLocale, $currencySign); } catch (I18nException $exception) { throw new ViewHelperException($exception->getMessage(), 1382350428, $exception); } } else { $output = number_format((double) $stringToFormat, 2, $decimalSeparator, $thousandsSeparator); if ($currencySign !== '') { $output .= ' ' . $currencySign; } } return $output; }
/** * @test * @dataProvider sampleDataForSpecificFormattingMethods */ public function specificFormattingMethodsWork($number, array $parsedFormat, $expectedResult, $formatType, $currencySign = null) { $mockNumbersReader = $this->createMock(I18n\Cldr\Reader\NumbersReader::class); $mockNumbersReader->expects($this->once())->method('parseFormatFromCldr')->with($this->sampleLocale, $formatType, 'default')->will($this->returnValue($parsedFormat)); $mockNumbersReader->expects($this->once())->method('getLocalizedSymbolsForLocale')->with($this->sampleLocale)->will($this->returnValue($this->sampleLocalizedSymbols)); $formatter = new I18n\Formatter\NumberFormatter(); $formatter->injectNumbersReader($mockNumbersReader); if ($formatType === 'currency') { $result = $formatter->formatCurrencyNumber($number, $this->sampleLocale, $currencySign); } else { $methodName = 'format' . ucfirst($formatType) . 'Number'; $result = $formatter->{$methodName}($number, $this->sampleLocale); } $this->assertEquals($expectedResult, $result); }