* 54321 * * ``` * * @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-format-currency */ final class CurrencyViewHelper extends AbstractViewHelper { /** * Output is escaped already. We must not escape children, to avoid double encoding. * * @var bool */ protected $escapeChildren = false; public function initializeArguments(): void { $this->registerArgument('currencySign', 'string', 'The currency sign, eg $ or €.', false, ''); $this->registerArgument('decimalSeparator', 'string', 'The separator for the decimal point.', false, ','); $this->registerArgument('thousandsSeparator', 'string', 'The thousands separator.', false, '.'); $this->registerArgument('prependCurrency', 'bool', 'Select if the currency sign should be prepended', false, false); $this->registerArgument('separateCurrency', 'bool', 'Separate the currency sign from the number by a single space, defaults to true due to backwards compatibility', false, true); $this->registerArgument('decimals', 'int', 'Set decimals places.', false, 2); $this->registerArgument('useDash', 'bool', 'Use the dash instead of decimal 00', false, false); } public function render(): string { $currencySign = $this->arguments['currencySign']; $decimalSeparator = $this->arguments['decimalSeparator']; $thousandsSeparator = $this->arguments['thousandsSeparator']; $prependCurrency = $this->arguments['prependCurrency']; $separateCurrency = $this->arguments['separateCurrency']; $decimals = (int)$this->arguments['decimals']; $useDash = $this->arguments['useDash']; $floatToFormat = $this->renderChildren(); if (empty($floatToFormat)) { $floatToFormat = 0.0; } else { $floatToFormat = (float)$floatToFormat; } $output = number_format($floatToFormat, $decimals, $decimalSeparator, $thousandsSeparator); if ($useDash && $floatToFormat === floor($floatToFormat)) { $output = explode($decimalSeparator, $output)[0] . $decimalSeparator . '—'; } if ($currencySign !== '') { $currencySeparator = $separateCurrency ? ' ' : ''; if ($prependCurrency === true) { $output = $currencySign . $currencySeparator . $output; } else { $output = $output . $currencySeparator . $currencySign; } } return $output; } }