Reputation: 743
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?
Upvotes: 8
Views: 20151
Reputation: 5885
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
.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
Upvotes: 6
Reputation: 4166
There is a jQuery plugin for it - https://github.com/dansingerman/jQuery-Browser-Language
See this SO answer for more info JavaScript for detecting browser language preference
Upvotes: 0