From eac9852c5cdc8a0f4734301b12da85252553c54a Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Mon, 14 Oct 2024 09:21:37 +0100 Subject: [PATCH 1/2] 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; From b1e9170888d88ebad87ca0ea335a4f6671aef9f8 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Tue, 15 Oct 2024 11:24:03 +0100 Subject: [PATCH 2/2] Update solution to be closed to the original --- src/helpers/Currency.php | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/helpers/Currency.php b/src/helpers/Currency.php index 177ca0ca27..d5ce390d84 100644 --- a/src/helpers/Currency.php +++ b/src/helpers/Currency.php @@ -106,22 +106,17 @@ public static function formatAsCurrency($amount, mixed $currency = null, bool $c if ($format) { $numberFormatter = new \NumberFormatter(Craft::$app->getFormattingLocale(), \NumberFormatter::CURRENCY); - $moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies()); - $money = Plugin::getInstance()->getCurrencies()->getTeller($currencyIso)->convertToMoney($amount); - $amount = $moneyFormatter->format($money); - } + // Strip zeros if requested and only if the amount won't have any decimal places + if ($stripZeros && (int)$amount == $amount) { + $numberFormatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 0); + $numberFormatter->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 0); + } - 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); + $moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies()); + $money = Plugin::getInstance()->getCurrencies()->getTeller($currencyIso)->convertToMoney($amount); - if (str_contains($amount, $zerosString)) { - $amount = str_replace($zerosString, '', $amount); - } + return $moneyFormatter->format($money); } return (string)$amount;