8

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?

dakab
  • 5,379
  • 9
  • 43
  • 67
coeurdange57
  • 715
  • 1
  • 8
  • 29
  • Possible duplicate of [How to format numbers and dates based on user locale settings?](http://stackoverflow.com/questions/11205873/how-to-format-numbers-and-dates-based-on-user-locale-settings) – Abhitalks Nov 06 '15 at 07:28
  • `2343.4 .toLocaleString() ` will show you the decimal in most browsers, and thousands in at least chrome... – dandavis Nov 06 '15 at 07:33

2 Answers2

6

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:

  • This does not get regional settings, but provides output in regional settings.
  • If you want your locale to be determined dynamically, use navigator.language.
  • There are many other means aside from this native approach; for starters, take a look at accounting.js or Stack Overflow answers like this one.

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
dakab
  • 5,379
  • 9
  • 43
  • 67
  • 1
    holy cow, when did this sneak into JS? even has IE11 support. awesome, just awesome. – dandavis Nov 06 '15 at 07:31
  • 18
    This does not answer the question. The question was how to retrieve the currency settings, not how to format a currency. – raarts Jun 30 '17 at 08:19
  • @raarts: Formatting was the actual goal (“format some numbers”), and since there is no (native) way to output applicable parameters, that was the first note I gave. Furthermore, if you need those “regional parameters” at all costs, you can always extract them: `let decimalSeparator = (1.23).toLocaleString(navigator.language,{style:'currency',currency:'EUR'}).match(/1(.)23/)[1];`. However, please note that I already put that into perspective, just read “Note that”. – dakab Jun 30 '17 at 09:03
  • I'm doing, `"$250".toLocaleLowerCase('en-GB', { style: 'currency', currency: 'EUR' })` but no matter what I try to enter the output is always $250. I've also tried `toLocaleString()` and I'm met with the same result. – Daniel Jackson Jan 15 '20 at 19:59
  • However I did get this to work `var formatter = new Intl.NumberFormat('en-GB', { style: 'currency', currency: 'EUR', }); console.log(formatter.format('2500'));` – Daniel Jackson Jan 15 '20 at 20:08
  • 1
    The NumberFormat options require that, if `style:currency` then `currency:`. Now, haw to find the current user's ? It has to be an ISO 4217 currency code. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat/NumberFormat#Syntax – Juan Lanus Feb 26 '20 at 16:36
0

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

Community
  • 1
  • 1
David Votrubec
  • 3,968
  • 3
  • 33
  • 44