Does JavaScript take local decimal separators into account?

前端 未结 7 807
陌清茗
陌清茗 2020-12-10 06:26

I\'ve got a web page that displays decimals in a user\'s localized format, like so:

  • English: 7.75
  • Dutch: 7,75

7条回答
  •  暖寄归人
    2020-12-10 07:04

    No, decimal separators are not localized at all in JavaScript, and parseFloat() parses numbers in the same format as you need to use in JavaScript source code: “.” as decimal separator, no group (thousands) separator, “E” or “e” as “times ten to power” symbol, and Ascii hyphen “-” as minus sign.

    To read or write numbers in localized format, you need something else. I would recommend the Globalize.js library, unless you can limit yourself to the single issue of decimal separator and a limited number of languages—in that case, it might be simpler to do just string manipulation that maps “.” to “,” on output and vice versa on input.

提交回复
热议问题