JavaScript - 如何将数字检测为十进制(包括 1.0)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7422072/
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
JavaScript - How To Detect Number As A Decimal (Including 1.0)
提问by Brian Grinstead
It feels like I am missing something obvious here. This has been asked a number of times - and the answer usually boils down to:
感觉就像我在这里遗漏了一些明显的东西。这已经被问过很多次了——答案通常归结为:
var num = 4.5;
num % 1 === 0; // false - 4.5 is a decimal
But, this fails for
但是,这失败了
var num = 1.0; // or 2.0, 3.0, ...
num % 1 // 0
Unfortunately, these doesn't work either
不幸的是,这些也不起作用
num.toString() // 1
typeof num // "number"
I am writing a JavaScript color parsing library, and I want to process input differently if it is given as 1.0 or 1. In my case, 1.0
really means 100% and 1
means 1.
我正在编写一个 JavaScript 颜色解析库,如果输入为 1.0 或 1,我想以不同的方式处理输入。在我的情况下,1.0
真正意味着 100% 并1
意味着 1。
Otherwise, both rgb 1 1 1
and rgb 1 255 255
will be parsed as rgb 255 255 255
(since I am right now taking anything <= 1 to mean a ratio).
否则,两个rgb 1 1 1
和rgb 1 255 255
将被解析为rgb 255 255 255
(因为我现在服用任何<= 1为表示比)。
采纳答案by jmar777
Those numbers aren't actually decimals or integers. They're all floats. The only real difference between 1
and 1.0
is the notation that was used to create floats of equal values.
这些数字实际上不是小数或整数。都是浮标。1
和之间唯一真正的区别1.0
是用于创建相等值的浮点数的符号。
编辑:为了帮助说明,请考虑:
1 === 1.0; // true
parseInt('1') == parseInt('1.0'); // true
parseFloat('1') === parseFloat('1.0'); // true
parseInt('1') === parseFloat('1'); // true
// etc...
Also, to demonstrate that they are really the same underlying data type:
此外,为了证明它们实际上是相同的底层数据类型:
typeof(1); // 'number'
typeof(1.0); // 'number'
Also, note that 'number' isn't unambiguous in JavaScript like it would be in other languages, because numbers are always floats.
另外,请注意“数字”在 JavaScript 中并不像在其他语言中那样明确,因为数字总是浮点数。
编辑 2:再添加一个,因为它是相关的。据我所知,在 JavaScript 中,您真正拥有“真实且真实”的整数而不是真正表示为浮点数的唯一上下文是在进行按位运算时。但是,在这种情况下,解释器将所有浮点数转换为整数,执行操作,然后在返回控制权之前将结果转换回浮点数。与这个问题并不完全相关,但它有助于对 JS 中的数字处理有一个很好的理解。
回答by Jose Faeti
Let your script parse the input as string, then it will be a matter of checking if there is the point like this.
让您的脚本将输入解析为字符串,然后检查是否有这样的点。
mystring.indexOf('.');
Check this exampleand this example.
回答by Jonathan M
You'll have to do it when parsing the string. If there's a decimal point in the string, treat it as percentage, otherwise it's just the integer value.
解析字符串时,您必须这样做。如果string 中有小数点,则将其视为百分比,否则它只是整数值。
So, e.g.:
所以,例如:
rgb 1 1 1 // same as #010101
rgb 1.0 1 1 // same as #ff0101
Since the rgb
is there, you're parsing the string anyway. Just look for .
in there as you're doing it.
既然rgb
存在,你无论如何都要解析字符串。只要.
在那里寻找就行了。
回答by Monkeyanator
Well, as far as the compiler is concerned, there is no difference between 1.0 and 1, and because there is no difference, it is impossible to tell the difference between them. You should change it from 1.0 to 100 for the the percentage thing. That might fix it.
好吧,就编译器而言,1.0和1之间没有区别,因为没有区别,所以无法分辨它们之间的区别。对于百分比,您应该将其从 1.0 更改为 100。那可能会解决它。
回答by Daniel Moses
var num = 1;
and
和
var num = 1.0;
are the same. You mention that you want to treat them differently when given from a user. You will want to parse the difference when it is still a string and convert it to the appropriate number.
是相同的。您提到您希望在用户提供时区别对待它们。当它仍然是一个字符串时,您将需要解析差异并将其转换为适当的数字。