Home > Web Front-end > JS Tutorial > body text

Bug problem occurs during conversion of Javascript function parseInt()_javascript skills

WBOY
Release: 2016-05-16 16:47:27
Original
1314 people have browsed it

This is a very strange thing, and I found it in today's test. parseInt(1.13*100), the actual return value is 112. Just look at the code

Copy the code The code is as follows:


< ;script type="text/javascript">
function test(){
var thisvalue = 0;
thisvalue=yuanToFen($("#inp").val());

alert(thisvalue);
}
/*yuan transfer points*/
function yuanToFen(yuan){
return parseInt(yuan*100,10);//Solution: change The return here is changed to: parseInt(yuan*1000,10)/10;
}









When you execute the above code, you will find that when you enter the period 1.13-1.16 When a number is entered, for example, 1.13 is entered, but the value output by the alert is 1.12. Similarly, if the input is 1.16, the alert output is 1.15.

But this will not happen when 2.13, 3.13, or 0.13 are input.

This bug is especially important for most accounting businesses.

For example: You want to remit money to someone, and the remittance unit is (100 million yuan). You remit 113 million yuan, but it actually turns out to be 112 million yuan. Ha ha. Just kidding. But the procedure must be rigorous. No matter if it is a penny or a tenth of a penny, it belongs to whoever it is. You must not be careless.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template