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

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

提交回复
热议问题