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

前端 未结 9 1838
花落未央
花落未央 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条回答
  •  猫巷女王i
    2020-12-03 07:22

    For javascript use the accounting library http://openexchangerates.github.io/accounting.js/ Then you can do:

    // Default usage:
    accounting.formatMoney(12345678); // $12,345,678.00
    
    // European formatting (custom symbol and separators), can also use     options object as second parameter:
    accounting.formatMoney(4999.99, "€", 2, ".", ","); // €4.999,99
    
    // Negative values can be formatted nicely:
    accounting.formatMoney(-500000, "£ ", 0); // £ -500,000
    
    // Simple `format` string allows control of symbol position (%v = value, %s = symbol):
    accounting.formatMoney(5318008, { symbol: "GBP",  format: "%v %s" }); // 5,318,008.00 GBP
    

提交回复
热议问题