JavaScript中的小数点分隔符号是什么? [英] What is the decimal separator symbol in JavaScript?
问题描述
当我编写一段处理一些浮点值的JavaScript代码时,一个想法让我感到震惊。 JavaScript中的小数点符号是什么?它总是。
?还是文化特有的?那么 .toFixed()
和 .parseFloat()
呢?如果我正在处理用户输入,则可能包含本地文化特定的小数点分隔符号。
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期望什么,我就无法编写这样的代码。
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.
已添加:好的,Rubens Farias 建议查看类似的问题,它有一个很好的接受答案:
Added: OK, Rubens Farias suggests to look at similar question which has a neat accepted answer:
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.
现在,剩下的部分将是确定 .parseFloat()$的行为c $ c>是。几个答案指出,对于浮点文字,只有
。
有效。 .parseFloat()
的行为方式是否相同?或者它可能在某些浏览器中需要本地小数点分隔符?是否有任何不同的方法来解析浮点数?我应该推出自己的应该吗?
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?
推荐答案
根据规范,DecimalLiteral定义为:
According to the specification, a DecimalLiteral is defined as:
DecimalLiteral ::
DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt
. DecimalDigits ExponentPartopt
DecimalIntegerLiteral ExponentPartopt
以及满足parseFloat参数:
and for satisfying the parseFloat argument:
- 让inputString为ToString(string)。
- 让trimmedString成为inputString的子串,由最左边的字符组成一个StrWhiteSpaceChar和该角色右边的所有字符。(换句话说,删除前导空格。)
- 如果trimmedString和trimmedString的任何前缀都不满足StrDecimalLiteral的语法(请参阅9.3.1),返回NaN。
- 让numberString成为trimmedString的最长前缀,它可能是trimmedString本身,它满足StrDecimalLiteral的语法。
- 返回MV的Number值
- 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
因此numberString成为trimmedString的最长前缀,满足StrDecimalLiteral的语法,意思是它找到的第一个可解析的文字字符串数字输入。只能使用。
来指定浮点数。如果您接受来自不同语言环境的输入,请使用字符串替换:
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:
function parseLocalNum(num) {
return +(num.replace(",", "."));
}
该函数使用一元运算符而不是parseFloat,因为在我看来你是想要严格的输入。 parseFloat(1ABC)
将是 1
,而使用一元运算符 +1ABC
返回 NaN
。这使得验证输入变得更加容易。使用parseFloat只是猜测输入格式正确。
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.
这篇关于JavaScript中的小数点分隔符号是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!