public render ( integer $decimals = 2, string $decimalSeparator = '.', string $thousandsSeparator = ',', string $localeFormatLength = NumbersReader::FORMAT_LENGTH_DEFAULT ) : string | ||
$decimals | integer | The number of digits after the decimal point |
$decimalSeparator | string | The decimal point character |
$thousandsSeparator | string | The character for grouping the thousand digits |
$localeFormatLength | string | Format length if locale set in $forceLocale. Must be one of Neos\Flow\I18n\Cldr\Reader\NumbersReader::FORMAT_LENGTH_*'s constants. |
return | string | The formatted number |
public function render($decimals = 2, $decimalSeparator = '.', $thousandsSeparator = ',', $localeFormatLength = NumbersReader::FORMAT_LENGTH_DEFAULT)
{
$stringToFormat = $this->renderChildren();
$useLocale = $this->getLocale();
if ($useLocale !== null) {
try {
$output = $this->numberFormatter->formatDecimalNumber($stringToFormat, $useLocale, $localeFormatLength);
} catch (I18nException $exception) {
throw new ViewHelperException($exception->getMessage(), 1382351148, $exception);
}
} else {
$output = number_format((double) $stringToFormat, $decimals, $decimalSeparator, $thousandsSeparator);
}
return $output;
}
/** * @test * @expectedException \Neos\FluidAdaptor\Core\ViewHelper\Exception */ public function viewHelperConvertsI18nExceptionsIntoViewHelperExceptions() { $localizationConfiguration = new \Neos\Flow\I18n\Configuration('de_DE'); $mockLocalizationService = $this->getMockBuilder(\Neos\Flow\I18n\Service::class)->setMethods(array('getConfiguration'))->getMock(); $mockLocalizationService->expects($this->once())->method('getConfiguration')->will($this->returnValue($localizationConfiguration)); $this->inject($this->viewHelper, 'localizationService', $mockLocalizationService); $mockNumberFormatter = $this->getMockBuilder(\Neos\Flow\I18n\Formatter\NumberFormatter::class)->setMethods(array('formatDecimalNumber'))->getMock(); $mockNumberFormatter->expects($this->once())->method('formatDecimalNumber')->will($this->throwException(new \Neos\Flow\I18n\Exception())); $this->inject($this->viewHelper, 'numberFormatter', $mockNumberFormatter); $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(123.456)); $this->viewHelper->setArguments(array('forceLocale' => true)); $this->viewHelper->render(); }