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

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

提交回复
热议问题