Hot to get user’s regional settings for currency i

2019-06-21 15:29发布

问题:

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?

回答1:

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:

  • It’s not like “get regional settings”, but “output in regional settings”.
  • Retrieval of currency depends on your use-case.
  • If you want your locale to be determined dynamically, use navigator.language.
  • There are lots of other means rather than this native approach; for starters, take a look at accounting.js or Stack Overflow answers like this one.


回答2:

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