I’m trying to format some numbers with jQuery. I would like to get the user’s regional settings for currency and number, in order to implement the correct format (obtain the decimal separator).
Is it possible to retrieve these parameters with jQuery or JavaScript?
The Intl. NumberFormat object enables language-sensitive number formatting.
Tip: To quickly apply the Currency format, select the cell or range of cells that you want to format, and then press Ctrl+Shift+$. Like the Currency format, the Accounting format is used for monetary values.
currency. js is a lightweight ~1kb javascript library for working with currency values. It was built to work around floating point issues in javascript. This talk by Bartek Szopka explains in detail why javascript has floating point issues.
JavaScript numbers can be formatted in different ways like commas, currency, etc. You can use the toFixed() method to format the number with decimal points, and the toLocaleString() method to format the number with commas and Intl. NumberFormat() method to format the number with currency.
Use Number.toLocaleString()
with style:'currency'
:
(73.57).toLocaleString('de-DE',{style:'currency',currency:'EUR'}); // German: 73,57 €
(73.57).toLocaleString('en-US',{style:'currency',currency:'EUR'}); // American: €73.57
Note that:
navigator.language
.As Daniel Jackson commented:
Using Intl.NumberFormat.format()
, you can achieve identical results, with the NumberFormat
and the general Intl
objects offering versatile options and methods with a main focus on language sensitivity.
new Intl.NumberFormat('de-DE',{style:'currency',currency:'EUR'}).format(73.57); // DE: 73,57 €
new Intl.NumberFormat('en-US',{style:'currency',currency:'EUR'}).format(73.57); // US: €73.57
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With