parseInt method of automatic calculation error on js

<script type="text/javascript">
	function test(){
					alert(parseInt("01")-parseInt("03"));
					alert(parseInt("02")-parseInt("03"));
					alert(parseInt("03")-parseInt("03"));
					alert(parseInt("04")-parseInt("03"));
					alert(parseInt("05")-parseInt("03"));
					alert(parseInt("06")-parseInt("03"));
					alert(parseInt("07")-parseInt("03"));
					alert(parseInt("08")-parseInt("03"));//结果为负
					alert(parseInt("09")-parseInt("03"));//结果为负
					alert(parseInt("10")-parseInt("03"));
					alert(parseInt("11")-parseInt("03"));
					alert(parseInt("12")-parseInt("03"));
	}

</script>
</head>
  <body>

  <a href="javascript:test()">点击</a>

  </body>
</html>

  

 

   Results were pop: -2 -101234-3-3789

  

After JS Find a document discovered the cause of this problem is in front of "0", parseInt method takes an optional parameter to represent binary digits, "0" as the first character string is recognized JS octal and no parameter value specified number of decimal parameter defaults to 8 so as to octal to parse the string, and "08" and "09" are not legal octal number, it is interpreted as a zero. </ P>

In fact, this is a problem caused by the attention to detail, parseInt explicitly set the parameters of the band will not have this problem, parseInt ( "08", 10) or parseInt ( "09", 10) can return the correct value . In addition, parseFloat will not have this problem

 

Reproduced in: https: //my.oschina.net/u/2260184/blog/540565

Guess you like

Origin blog.csdn.net/weixin_34128411/article/details/92186186