开发者

Javascript Number and Currency localization [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 5 years ago.

开发者_开发百科 Improve this question

I've run into numbers and currency localization in JavaScript

What I need is a convenient library for that.

I am responsible for setting the decimal separators, currency, etc.

Please post the best links you think


Most modern browsers have built in support internationalisation in the form of the global Intl object and extensions to Number, String & Date.

var money = 123456.12;

// display with correct formatting
money.toLocaleString('de-DE'); // "123.456,12"

// for currency, bad as we're leaving the precision to the gods of floating point numbers
money.toLocaleString('en-GB', { style: 'currency', currency: 'GBP' }); // "£123,456.12"

// for currency, good as we're using strings...
new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format('12312.12')

If you're not familiar with why floating point numbers are bad for currency info check this out on floating point numbers


The best answer for you probably depends on what javascript libary, if any, you are currently using. But YUI has support for number/currency formatting with internationalization, and it is a solid and well-designed library.

Example:

alert(Y.DataType.Number.format(123123123.176,{
    prefix: "€",
    thousandsSeparator: ".",
    decimalSeparator: ",",
    decimalPlaces: 2,
    suffix: " (EUR)"
}));


Microsoft has created a useful plugin for jquery:

http://weblogs.asp.net/scottgu/archive/2010/06/10/jquery-globalization-plugin-from-microsoft.aspx


This post is quite old, but I post a response in case it's interesting someone.

I found the numeral.js library pretty useful to do this.
You can define your custom formats and add localized files in the same way that 'moment.js'.

You should probably check to see if it fits your needs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜