Skip to content

[5.x] Add roundCurrency helper to round correctly for any currency#1250

Open
Jade-GG wants to merge 1 commit intomasterfrom
Jade-GG-patch-1
Open

[5.x] Add roundCurrency helper to round correctly for any currency#1250
Jade-GG wants to merge 1 commit intomasterfrom
Jade-GG-patch-1

Conversation

@Jade-GG
Copy link
Copy Markdown
Collaborator

@Jade-GG Jade-GG commented Apr 3, 2026

Ref: none

The sumPrices function relied on the fact that most currencies have 2 decimals. This will work, until you run into a currency like Yen (0 decimals) or Dinar (3 decimals)1

By using Intl.NumberFormat and getting the maximumFractionDigits from that, we can round properly. I've made a helper function for this and applied it to the sumPrices function.

Footnotes

  1. https://en.wikipedia.org/wiki/ISO_4217#List_of_ISO_4217_currency_codes

Comment thread resources/js/helpers.js
Comment on lines +35 to +39
const formatter = new Intl.NumberFormat([(config.locale ?? 'default').replace('_', '-'), 'default'], {
style: 'currency',
currency: config.currency ?? 'eur',
})
const digits = formatter.resolvedOptions().maximumFractionDigits
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could save this in a variable to reduce the amount of numberformatter objects instantiated for each roundCurrency call

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants