Javascript parseInt() 和 parseFloat() 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12812863/
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
difference between parseInt() and parseFloat()
提问by Iam_Signal
Possible Duplicate:
Behavior difference between parseInt() and parseFloat()
var box = $('.box'),
fontSize = parseInt(box.css('font-size'), 10) + 5;
$('button').on('click', function() {
box.animate({fontSize: fontSize});
});
//..
//..
var box = $('.box'),
fontSize = parseFloat(box.css('font-size'), 10) + 5;
$('button').on('click', function() {
box.animate({fontSize: fontSize})
});
what the difference between..
有什么区别。。
**fontSize = parseInt(box.css('font-size'), 10);**
**fontSize = parseFloat(box.css('font-size'), 10);**
and and why should put 10 as a context..Please Help?
以及为什么要把 10 作为上下文..请帮忙?
回答by Techie
JavaScript provides two methods for converting non-number primitives into numbers: parseInt() and parseFloat() . As you may have guessed, the former converts a value into an integer whereas the latter converts a value into a floating-point number.
JavaScript 提供了两种将非数字原语转换为数字的方法: parseInt() 和 parseFloat() 。您可能已经猜到,前者将值转换为整数,而后者将值转换为浮点数。
Any number literal contained in a string is also converted correctly, so the string "0xA" is properly converted into the number 10. However, the string "22.5" will be converted to 22 , because the decimal point is an invalid character for an integer. Some examples:
字符串中包含的任何数字文字也会正确转换,因此字符串 "0xA" 正确转换为数字 10。但是,字符串 "22.5" 将转换为 22 ,因为小数点是整数的无效字符. 一些例子:
var iNum1 = parseInt("1234blue"); //returns 1234
var iNum2 = parseInt("0xA"); //returns 10
var iNum3 = parseInt("22.5"); //returns 22
var iNum4 = parseInt("blue"); //returns NaN
var iNum1 = parseInt("1234blue"); //返回1234
var iNum2 = parseInt("0xA"); //返回10
var iNum3 = parseInt("22.5"); //返回22
var iNum4 = parseInt("blue"); //返回NaN
The parseInt() method also has a radix mode, allowing you to convert strings in binary, octal, hexadecimal, or any other base into an integer. The radix is specified as a second argument to parseInt() , so a call to parse a hexadecimal value looks like this:
parseInt() 方法还具有基数模式,允许您将二进制、八进制、十六进制或任何其他基数的字符串转换为整数。基数被指定为 parseInt() 的第二个参数,因此解析十六进制值的调用如下所示:
var iNum1 = parseInt("AF", 16); //returns 175
Of course, this can also be done for binary, octal, and even decimal (which is the default mode):
var iNum1 = parseInt("10", 2); //returns 2
var iNum2 = parseInt("10", 8); //returns 8
var iNum2 = parseInt("10", 10); //returns 10
var iNum1 = parseInt("AF", 16); //返回175
当然,对于二进制、八进制甚至十进制(默认模式)也可以这样做:
var iNum1 = parseInt("10", 2); //返回2
var iNum2 = parseInt("10", 8); //返回8
var iNum2 = parseInt("10", 10); //返回10
If decimal numbers contain a leading zero, it's always best to specify the radix as 10 so that you won't accidentally end up with an octal value. For example:
如果十进制数包含前导零,最好将基数指定为 10,这样您就不会意外地得到八进制值。例如:
var iNum1 = parseInt("010"); //returns 8
var iNum2 = parseInt("010", 8); //returns 8
var iNum3 = parseInt("010", 10); //returns 10
In this code, both lines are parsing the string "010" into a number. The first line thinks that the string is an octal value and parses it the same way as the second line (which specifies the radix as 8). The last line specifies a radix of 10, so iNum3 ends up equal to 10.
var iNum1 = parseInt("010"); //返回8
var iNum2 = parseInt("010", 8); //返回8
var iNum3 = parseInt("010", 10); //返回10
在这段代码中,两行都将字符串“010”解析为一个数字。第一行认为该字符串是一个八进制值,并以与第二行(指定基数为 8)相同的方式对其进行解析。最后一行指定基数为 10,因此 iNum3 最终等于 10。
Another difference when using parseFloat() is that the string must represent a floating-point number in decimal form, not octal or hexadecimal. This method ignores leading zeros, so the octal number 0908 will be parsed into 908 , and the hexadecimal number 0xA will return NaN because x isn't a valid character for a floating-point number. There is also no radix mode for parseFloat() .
使用 parseFloat() 时的另一个区别是字符串必须以十进制形式表示浮点数,而不是八进制或十六进制。此方法忽略前导零,因此八进制数 0908 将被解析为 908 ,十六进制数 0xA 将返回 NaN,因为 x 不是浮点数的有效字符。parseFloat() 也没有基数模式。
Some examples of using parseFloat() :
var fNum1 = parseFloat("1234blue"); //returns 1234
var fNum2 = parseFloat("0xA"); //returns 0
var fNum3 = parseFloat("22.5"); //returns 22.5
var fNum4 = parseFloat("22.34.5"); //returns 22.34
var fNum5 = parseFloat("0908"); //returns 908
var fNum6 = parseFloat("blue"); //returns NaN
使用 parseFloat() 的一些示例:
var fNum1 = parseFloat("1234blue"); //返回1234
var fNum2 = parseFloat("0xA"); //返回0
var fNum3 = parseFloat("22.5"); //返回22.5
var fNum4 = parseFloat("22.34.5"); //返回 22.34
var fNum5 = parseFloat("0908"); //返回908
var fNum6 = parseFloat("蓝色"); //返回NaN
回答by Arkadiusz 'flies' Rzadkowolski
First of all only parseInt
accepts second argument. It's called radix. It represents numeral system to be used. In example you can convert number into binary or hexadecimal code.
首先只parseInt
接受第二个参数。它被称为基数。它代表要使用的数字系统。例如,您可以将数字转换为二进制或十六进制代码。
parseFloat
only accepts one argument.
parseFloat
只接受一个参数。