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

前端 未结 9 1839
花落未央
花落未央 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:23

    The accepted answer from Matt Ball is wrong - dunno why nobody haven't noticed. There is no such function as String.toLocaleString() [ref]! Therefore when Number.toFixed() returns String, the consequent toLocaleString() does nothing. So you won't get localized number, just the product of toFixed() function.

    WRONG (don't do it like this):

    var i = 1234.123;
    alert(i.toFixed(2).toLocaleString() + ' €'); // ALWAYS alerts '1234.12 €' (no locale formatting)
    

    Suggestion how to do it right:

    You may use jQuery plugin like NumberFormatter.

提交回复
热议问题