Javascript number(x) 和 parseFloat(x) 哪个更好?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12227594/
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
Which is better, number(x) or parseFloat(x)?
提问by Namanyay Goel
Which is better?
哪个更好?
I'm asking this just for the sake of shaving a few bytes, as I can use +x instead of number(x). Does parsefloat do something better?
我问这个只是为了减少几个字节,因为我可以使用 +x 而不是 number(x)。parsefloat 是否做得更好?
回答by Nathan Wall
The difference between parseFloat and Number
parseFloat 和 Number 的区别
parseFloat
/parseInt
is for parsing a string, while Number
/+
is for coercing a value to a number. They behave differently. But first let's look at where they behave the same:
parseFloat
/parseInt
用于解析字符串,而Number
/+
用于将值强制转换为数字。他们的行为不同。但首先让我们看看它们的行为相同的地方:
parseFloat('3'); // => 3
Number('3'); // => 3
parseFloat('1.501'); // => 1.501
Number('1.501'); // => 1.501
parseFloat('1e10'); // => 10000000000
Number('1e10'); // => 10000000000
So as long as you have standard numeric input, there's no difference. However, if your input starts with a number and then contains other characters, parseFloat
truncates the number out of the string, while Number
gives NaN
(not a number):
所以只要你有标准的数字输入,就没有区别。但是,如果您的输入以数字开头,然后包含其他字符,parseFloat
则从字符串中截断数字,而Number
给出NaN
(不是数字):
parseFloat('1x'); // => 1
Number('1x'); // => NaN
In addition, Number
understands hexadecimal input while parseFloat
does not:
此外,Number
理解十六进制输入而parseFloat
不能:
parseFloat('0x10'); // => 0
Number('0x10'); // => 16
But Number
acts weird with empty strings or strings containing only white space:
但是Number
对于空字符串或仅包含空格的字符串表现得很奇怪:
parseFloat(''); // => NaN
Number(''); // => 0
parseFloat(' \r\n\t'); // => NaN
Number(' \r\n\t'); // => 0
On the whole, I find Number
to be more reasonable, so I almost always use Number
personally (and you'll find that a lot of the internal JavaScript functions use Number
as well). If someone types '1x'
I prefer to show an error rather than treat it as if they had typed '1'
. The only time I really make an exception is when I am converting a style to a number, in which case parseFloat
is helpful because styles come in a form like '3px'
, in which case I want to drop the 'px'
part and just get the 3
, so I find parseFloat
helpful here. But really which one you choose is up to you and which forms of input you want to accept.
总的来说,我觉得Number
更合理,所以我几乎总是Number
个人使用(你会发现很多内部 JavaScript 函数也使用Number
)。如果有人'1x'
输入,我更愿意显示错误,而不是将其视为他们输入了'1'
. 我真正例外的唯一一次是当我将样式转换为数字时,在这种情况下parseFloat
很有帮助,因为样式的形式类似于'3px'
,在这种情况下,我想删除'px'
部分并只获取3
,所以我觉得parseFloat
很有帮助这里。但实际上,您选择哪一种取决于您以及您想接受哪种形式的输入。
Note that using the unary +
operator is exactly the same as using Number
as a function:
请注意,使用一元运算+
符与作为Number
函数使用完全相同:
Number('0x10'); // => 16
+'0x10'; // => 16
Number('10x'); // => NaN
+'10x'; // => NaN
Number('40'); // => 40
+'40'; // => 40
So I usually just use +
for short. As long as you know what it does, I find it easy to read.
所以我通常只是+
简称。只要你知道它的作用,我觉得它很容易阅读。
回答by Jon
The difference is what happens when the input is not a "proper number". Number
returns NaN
while parseFloat
parses "as much as it can". If called on the empty string Number
returns 0
while parseFloat returns NaN
.
不同之处在于当输入不是“正确的数字”时会发生什么。在解析“尽可能多”时Number
返回。如果调用空字符串返回,而 parseFloat 返回。NaN
parseFloat
Number
0
NaN
For example:
例如:
Number("") === 0 // also holds for false
isNaN(parseFloat("")) === true // and null
isNaN(Number("32f")) === true
parseFloat("32f") === 32
回答by micnic
In these examples you can see the difference:
在这些示例中,您可以看到不同之处:
Number('') = 0;
Number(false) = 0;
Number('1a') = NaN;
parseFloat('') = NaN;
parseFloat(false) = NaN;
parseFloat('1a') = 1;
parseFloat is a bit slower because it searches for first appearance of a number in a string, while the Number constuctor creates a new number instance from strings that contains numeric values with whitespace or that contains false values.
parseFloat 有点慢,因为它搜索字符串中数字的第一次出现,而 Number 构造器从包含带有空格的数值或包含假值的字符串创建一个新的数字实例。
P.S. If you are interested in some universal type conversion solutions you can read the post about type conversion in my blog: http://justsimplejs.blogspot.com/2012/08/data-type-conversion.html
PS 如果你对一些通用的类型转换解决方案感兴趣,你可以阅读我博客中关于类型转换的文章:http: //justsimplejs.blogspot.com/2012/08/data-type-conversion.html
回答by xdazz
For empty string, they are different.
对于空字符串,它们是不同的。
+""
and Number("")
returns 0, while parseFloat("")
returns NaN.
+""
并Number("")
返回 0,而parseFloat("")
返回 NaN。
回答by Christopher
As far as I know, and this is only overheard from colleagues so might be entirely ill informed, that parseFloat is marginally faster.
据我所知,这只是从同事那里听到的,所以可能完全不知情, parseFloat 稍微快一点。
Though on further researching, it would seem that this performance difference is browser dependant.
尽管经过进一步研究,这种性能差异似乎取决于浏览器。
http://jsperf.com/parseint-vs-parsefloat/6
http://jsperf.com/parseint-vs-parsefloat/6
Have a look at these jsPerf results, and make you're call. (it includes +x tests as well)
看看这些 jsPerf 结果,然后让你打电话。(它还包括 +x 测试)
As noted in @xdazz 's answer, +""
and Number("")
return 0
while parseFloat("")
returns NaN
so Again I would go with parseFloat, because an empty string does NOT mean the number 0, only a string with the character "0"
in it means 0;
正如@xdazz指出的回答,+""
并Number("")
返回0
而parseFloat("")
返回NaN
所以我要再次去parseFloat,因为一个空字符串并不代表数字0,只用字符的字符串"0"
中这意味着0;