base_currency = $base_currency; $this->locale = Locale::getDefault(); } /** * 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($value, ?Currency $currency = null, $decimals = 5, bool $show_all_digits = false) { $iso_code = $this->base_currency; if (null !== $currency && ! empty($currency->getIsoCode())) { $iso_code = $currency->getIsoCode(); } $number_formatter = new NumberFormatter($this->locale, NumberFormatter::CURRENCY); 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); } }