Skip to content

Commit

Permalink
Merge pull request #3720 from craftcms/feature/pt-2202-5x-commercecur…
Browse files Browse the repository at this point in the history
…rencyeurstripzerostrue-does-not-strip-zeros

Fixed #3716 currency helper strip zeros
  • Loading branch information
nfourtythree authored Oct 15, 2024
2 parents 2308a21 + b1e9170 commit 8e9533f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
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
7 changes: 7 additions & 0 deletions src/helpers/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ public static function formatAsCurrency($amount, mixed $currency = null, bool $c

if ($format) {
$numberFormatter = new \NumberFormatter(Craft::$app->getFormattingLocale(), \NumberFormatter::CURRENCY);

// 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);
}

$moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies());
$money = Plugin::getInstance()->getCurrencies()->getTeller($currencyIso)->convertToMoney($amount);

Expand Down

0 comments on commit 8e9533f

Please sign in to comment.