Skip to content

Commit

Permalink
Fixed #3716 currency helper strip zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Oct 14, 2024
1 parent 2308a21 commit eac9852
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 13 additions & 1 deletion src/helpers/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit eac9852

Please sign in to comment.