Javascript function parseInt of has a bug while converting

  • 2020-03-30 03:01:52
  • OfStack

This is a very weird thing, as measured by today's test. ParseInt (1.13*100), the actual return value is 112. Just look at the code
 
<head> 
<script type="text/javascript"> 
function test(){ 
var thisvalue = 0; 
thisvalue=yuanToFen($("#inp").val()); 

alert(thisvalue); 
} 
 
function yuanToFen(yuan){ 
return parseInt(yuan*100,10);//Solution: change the return here to: parseInt(yuan*1000,10)/10;
} 
</script> 
</head> 
<body> 
<div> 
<input type="text" id="inp" name="pl" value="1.14"/><!--  When the input value is: 1.13 , 1.14 , 1.15 , 1.16 When the bug No other data has been found so far bug --> 
<input type="button" name="ok" value=" confirm " onclick="test();"/> 
</div> 
</body> 
</html> 

When you execute the above code, you will find that when you enter a number between 1.13 and 1.16, for example, you enter 1.13, but the alert value is 1.12. Likewise, if you enter 1.16, the alert value is 1.15.

But when input 2.13, 3.13, 0.13 will not appear such a situation.

This bug is especially important for the most bookkeeping businesses.

For example: you want to give so and so person remittance, remittance unit is (100 million yuan), you remit 113 million, the result actually becomes 112 million. Ha ha. Just kidding. But the process is rigorous, regardless of a penny or a penny is who is who, must not be sloppy.

Related articles: