JavaScript 中的小数点分隔符是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2085275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the decimal separator symbol in JavaScript?
提问by Vilx-
A thought struck me as I was writing a piece of JavaScript code that processed some floating point values. What is the decimal point symbol in JavaScript? Is it always .? Or is it culture-specific? And what about .toFixed()and .parseFloat()? If I'm processing a user input, it's likely to include the local culture-specific decimal separator symbol.
当我编写一段处理一些浮点值的 JavaScript 代码时,我突然想到一个想法。JavaScript 中的小数点符号是什么?总是这样.吗?或者它是特定于文化的?那么.toFixed()和.parseFloat()呢?如果我正在处理用户输入,它可能包含本地文化特定的小数分隔符符号。
Ultimately I'd like to write code that supports both decimal points in user input - culture-specific and ., but I can't write such a code if I don't know what JavaScript expects.
最终,我想编写在用户输入中同时支持小数点的代码 - 特定于文化的和.,但如果我不知道 JavaScript 期望什么,我就无法编写这样的代码。
Added:OK, Rubens Farias suggeststo look at similar questionwhich has a neat accepted answer:
补充:好的,Rubens Farias建议查看类似的问题,该问题有一个简洁的公认答案:
function whatDecimalSeparator() {
var n = 1.1;
n = n.toLocaleString().substring(1, 2);
return n;
}
That's nice, it lets me get the locale decimal point. A step towards the solution, no doubt.
太好了,它让我可以得到语言环境的小数点。毫无疑问,朝着解决方案迈出了一步。
Now, the remaining part would be to determine what the behavior of .parseFloat()is. Several answers point out that for floating point literals only .is valid. Does .parseFloat()act the same way? Or might it require the local decimal separator in some browser? Are there any different methods for parsing floating point numbers as well? Should I roll out my own just-to-be-sure?
现在,剩下的部分是确定 的行为.parseFloat()是什么。几个答案指出,仅对浮点文字.有效。是否.parseFloat()以同样的方式操作?或者它可能需要在某些浏览器中使用本地小数点分隔符?是否有任何不同的方法来解析浮点数?我应该推出我自己的只是为了确定吗?
采纳答案by Andy E
According to the specification, a DecimalLiteral is defined as:
根据规范,DecimalLiteral 定义为:
DecimalLiteral ::
DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt
. DecimalDigits ExponentPartopt
DecimalIntegerLiteral ExponentPartopt
and for satisfying the parseFloat argument:
并且为了满足 parseFloat 参数:
- Let inputString be ToString(string).
- Let trimmedString be a substring of inputString consisting of the leftmost character that is not a StrWhiteSpaceChar and all characters to the right of that character.(In other words, remove leading white space.)
- If neither trimmedString nor any prefix of trimmedString satisfies the syntax of a StrDecimalLiteral (see 9.3.1), return NaN.
- Let numberString be the longest prefix of trimmedString, which might be trimmedString itself, that satisfies the syntax of a StrDecimalLiteral.
- Return the Number value for the MV
- 令 inputString 为 ToString(string)。
- 让trimmedString 是inputString 的一个子字符串,它由最左边的不是StrWhiteSpaceChar 的字符和该字符右边的所有字符组成。(换句话说,删除前导空格。)
- 如果trimmedString 和trimmedString 的任何前缀都不满足StrDecimalLiteral 的语法(参见9.3.1),则返回NaN。
- 令numberString 为trimmedString 的最长前缀,它可能是trimmedString 本身,满足StrDecimalLiteral 的语法。
- 返回 MV 的 Number 值
So numberString becomes the longest prefix of trimmedString that satisfies the syntax of a StrDecimalLiteral, meaning the first parseable literal string number it finds in the input. Only the .can be used to specify a floating-point number. If you're accepting inputs from different locales, use a string replace:
因此,numberString 成为trimmedString 的最长前缀,它满足StrDecimalLiteral 的语法,这意味着它在输入中找到的第一个可解析的文字字符串编号。只有.可用于指定浮点数。如果您接受来自不同语言环境的输入,请使用字符串替换:
function parseLocalNum(num) {
return +(num.replace(",", "."));
}
The function uses the unary operator instead of parseFloat because it seems to me that you want to be strict about the input. parseFloat("1ABC")would be 1, whereas using the unary operator +"1ABC"returns NaN. This makes it MUCH easier to validate the input. Using parseFloat is just guessing that the input is in the correct format.
该函数使用一元运算符而不是 parseFloat ,因为在我看来您希望对输入严格。 parseFloat("1ABC")将是1,而使用一元运算符+"1ABC"返回NaN。这使得验证输入变得更加容易。使用 parseFloat 只是猜测输入的格式正确。
回答by jspcal
use:
用:
theNumber.toLocaleString();
to get a properly formatted string with the right decimal and thousands separators
使用正确的小数点和千位分隔符获取格式正确的字符串
回答by Matt Sach
As far as I'm aware, javascript itself only knows about the .separator for decimals. At least one person whose judgement I trust on JS things concurs:
据我所知,javascript 本身只知道.小数的分隔符。至少有一个我相信他对 JS 事物的判断的人同意:

