From eac9852c5cdc8a0f4734301b12da85252553c54a Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Mon, 14 Oct 2024 09:21:37 +0100 Subject: [PATCH] Fixed #3716 currency helper strip zeros --- CHANGELOG.md | 1 + src/helpers/Currency.php | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00b0feb95f..54ac70f2db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Fixed a bug where inventory items could appear with blank descriptions on the Inventory management screen. ([#3706](https://github.com/craftcms/commerce/issues/3706)) - Fixed a bug where additional buttons defined with `Order::EVENT_DEFINE_ADDITIONAL_BUTTONS` weren’t displayed on the Edit Order screen. ([#3692](https://github.com/craftcms/commerce/issues/3692)) - Fixed a bug where email errors weren’t displayed on the Edit Order screen. ([#3693](https://github.com/craftcms/commerce/issues/3693)) +- Fixed a bug where `craft\commerce\helpers\Currency::formatAsCurrency()` wasn't stripping zeros. ([#3716](https://github.com/craftcms/commerce/issues/3716)) ## 5.1.3 - 2024-10-02 diff --git a/src/helpers/Currency.php b/src/helpers/Currency.php index 895e685e17..177ca0ca27 100644 --- a/src/helpers/Currency.php +++ b/src/helpers/Currency.php @@ -109,7 +109,19 @@ public static function formatAsCurrency($amount, mixed $currency = null, bool $c $moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies()); $money = Plugin::getInstance()->getCurrencies()->getTeller($currencyIso)->convertToMoney($amount); - return $moneyFormatter->format($money); + $amount = $moneyFormatter->format($money); + } + + if ($stripZeros) { + $decimalSeparator = Craft::$app->getFormattingLocale()->getNumberSymbol(\craft\i18n\Locale::SYMBOL_DECIMAL_SEPARATOR); + // find decimal separator and zeros based on formatting locale and number of subunits + $subUnit = Plugin::getInstance()->getCurrencies()->getSubunitFor($currencyIso); + $zeroSymbol = Craft::$app->getFormattingLocale()->getNumberSymbol(\craft\i18n\Locale::SYMBOL_ZERO_DIGIT); + $zerosString = $decimalSeparator . str_repeat($zeroSymbol, $subUnit); + + if (str_contains($amount, $zerosString)) { + $amount = str_replace($zerosString, '', $amount); + } } return (string)$amount;