diff --git a/src/Services/MoneyFormatter.php b/src/Services/MoneyFormatter.php index b3dc8d33..a1e933c7 100644 --- a/src/Services/MoneyFormatter.php +++ b/src/Services/MoneyFormatter.php @@ -48,12 +48,14 @@ class MoneyFormatter } /** - * @param string $value The value that should be - * @param Currency|null $currency - * @param int $decimals + * Format the the given value in the given currency + * @param string|float $value The value that should be formatted. + * @param Currency|null $currency The currency that should be used for formatting. If null the global one is used + * @param int $decimals The number of decimals that should be shown. + * @param bool $show_all_digits If set to true, all digits are shown, even if they are null. * @return string */ - public function format(string $value, ?Currency $currency = null, $decimals = 5) + public function format($value, ?Currency $currency = null, $decimals = 5, bool $show_all_digits = false) { $iso_code = $this->base_currency; if ($currency !== null && !empty($currency->getIsoCode())) { @@ -61,7 +63,11 @@ class MoneyFormatter } $number_formatter = new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY); - $number_formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $decimals); + if ($show_all_digits) { + $number_formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $decimals); + } else { + $number_formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $decimals); + } return $number_formatter->formatCurrency((float) $value, $iso_code); } diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index 966fb126..ff92f8ef 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -130,9 +130,9 @@ class AppExtension extends AbstractExtension return $this->moneyFormatter->format($amount, $currency, $decimals); } - public function siFormat($value, $unit, $decimals = 2) + public function siFormat($value, $unit, $decimals = 2, bool $show_all_digits = false) { - return $this->siformatter->format($value, $unit, $decimals); + return $this->siformatter->format($value, $unit, $decimals, $show_all_digits); } public function amountFormat($value, ?MeasurementUnit $unit, array $options = [])