本文介绍了parseInt()和parseFloat()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





  var box = $('。box'),
fontSize = parseInt(box.css('font-size'),10)+ 5; ('click',function(){
box.animate({fontSize:fontSize});
});

$('button')。

// ..

  var box = $('。box'),
fontSize = parseFloat(box.css('font-size'),10)+ 5; ('click',function(){
box.animate({fontSize:fontSize})
});

$('button')。

有什么区别..



<$ p (box.css('font-size'),10); **

** fontSize = parseFloat(box.css(' font-size'),10); **

以及为什么要把10作为上下文。 。

解决方案

字符串中包含的任何数字字面值也被正确转换,所以字符串0xA被正确地转换为数字10.但是,字符串22.5将被转换为22,因为小数点点是一个整数的无效字符。一些例子:

parseInt()方法也有一个基数模式,允许您以二进制,八进制,十六进制,或任何其他基地转换为整数。基数被指定为parseInt()的第二个参数,因此解析十六进制值的调用如下所示:

如果十进制数字包含前导零,最好将基数指定为10,这样您就不会意外地结束了一个八进制值。例如:

使用parseFloat( )是该字符串必须以十进制形式表示浮点数,而不是八进制或十六进制。此方法忽略前导零,因此八进制数字0908将被解析为908,十六进制数字0xA将返回NaN,因为x不是浮点数的有效字符。 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?

解决方案

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:

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:

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:

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() .

Read More,Read More

Similar Question Read more here

这篇关于parseInt()和parseFloat()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 16:58