Hot to get regional user settings for currency in JavaScript or jQuery?

I am trying to format some numbers using jQuery. I would like to get regional user settings for the currency and number in order to implement the correct format (get the decimal separator).

Is it possible to get these parameters using jQuery or JavaScript?

+5
source share
2 answers

Use toLocaleString() with style:'currency' :

 var amount = 123.56; alert( 'German: ' + amount.toLocaleString('de-DE',{style:'currency',currency:'EUR'}) + ', ' + 'American: ' + amount.toLocaleString('en-US',{style:'currency',currency:'EUR'}) ); // German: 123,56 € // American: €123.56 

Note that:

  • Not like getting regional settings, but output in regional settings.
  • Getting a currency depends on your use case.
  • If you want your language to be defined dynamically, use navigator.language .
  • There are many other remedies, not this native approach; For starters, take a look at accounting.js or stack overflow answers like this one.
+4
source

There is a jQuery plugin for it - https://github.com/dansingerman/jQuery-Browser-Language

See this SO answer for more JavaScript info for determining browser preferences.

+1
source

Source: https://habr.com/ru/post/1235344/


All Articles