Is there a functionality in JavaScript to convert values into specific locale formats?

前端 未结 9 1786
花落未央
花落未央 2020-12-03 06:32

Is there a built in function of JavaScript to convert a string into a particular locale (Euro in my case)?

E.g. 50.00 should get converted to 50,0

相关标签:
9条回答
  • 2020-12-03 07:00

    50.00 is a unit-less value. The best you can do is convert 50.00 to 50,00 and then append the yourself. Therefore, just use Number.toLocaleString().

    var i = 50.00;
    alert(i.toLocaleString() + ' €'); // alerts '50.00 €' or '50,00 €'
    

    Demo →

    Lots of relevant questions:

    • How can I format numbers as money in JavaScript? (the big one; ~70k views)
    • Convert to currency format
    • Format currency using javascript
    • how do i print currency format in javascript
    • JavaScript: Format number/currency w/regards to culture like .NET's String.Format()? (possibly useful, if you're using ASP.NET)
    • format number to price
    0 讨论(0)
  • 2020-12-03 07:02

    Some of the other answers are okay but I would recommend a different library, NumeralJS over Number.toLocaleString because the latter is not supported widely in all browsers (it breaks on even new versions of safari). NumeralJS is really powerful and supports anything you would want to do with converting numbers to strings.

    First, set the language (I chose french) and then format it:

    numeral.language('fr');
    numeral(50.00).format('0.00 $');
    

    Outputs:

    "50,00 €"
    

    The website explains it quite well and has tons of examples.

    0 讨论(0)
  • 2020-12-03 07:04

    Built-in, yes and no. There is Number.toLocaleString() but it is dependent on the system's locale.

    However, there are some libraries which have modules for this. MooTools's Locale.Number for example allows you to convert a number into different locales (adding your own locale is trivial).

    Locale.use("EU");
    var inEuros = (50).formatCurrency(); // € 50,00
    

    jsFiddle Demo


    If you want the € sign to be printed after, you can simply create your own locale:

    Locale.define('EU-suffix', 'Number', {
        currency: {
            suffix: ' €'
        }
    }).inherit('EU', 'Number');
    
    0 讨论(0)
  • 2020-12-03 07:06

    I found a way to do this at this page.

    You can you toLocaleString without using toFixed before it. toFixed returns a string, toLocaleString should get a number. But you can pass an options object with toLocaleString, the option minimumFractionDigits could help you with the functionality toFixed has.

    50.toLocaleString('de-DE', {
        style: 'currency', 
        currency: 'EUR', 
        minimumFractionDigits: 2 
    });
    

    Checkout all the other options you can pass with this function.

    0 讨论(0)
  • 2020-12-03 07:07

    There are locale related features described within the ECMAScript Internationalization API.

    To get the float 50.0 converted to the string 50,00 € (using the 'de-DE' locale) you need to write this:

    new Intl.NumberFormat("de-DE", {style: "currency", currency: "EUR"}).format(50.0)
    

    This API is available in all current major browsers.

    For more info about the number formatting features of the Internationalization API you should read the article at MDN.

    0 讨论(0)
  • 2020-12-03 07:10

    I have prepared small library to deal with currency formatting - money

    money(1000.5, 'EUR');   // -> 1 000.50 €
    money(1000.5, 'USD');   // -> $1,000.50
    money(1000.5, 'PLN');   // -> 1 000,50 zł
    

    Supported currency (ISO codes): PLN, EUR, USD, GBP, JPY, CZK, SEK

    0 讨论(0)
提交回复
热议问题