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

萝らか妹 提交于 2019-12-10 13:54:40

问题


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



来源:https://stackoverflow.com/questions/33561412/hot-to-get-user-s-regional-settings-for-currency-in-javascript-or-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!