Home
Web Front-end
JS Tutorial
Example analysis of float operation precision in javascript_javascript skills



Example analysis of float operation precision in javascript_javascript skills
May 16, 2016 pm 06:20 PM
float
Someone asked a js question:
Copy code The code is as follows:
var i = 0.07;
var r = i*100;
alert(r);
Why is the result 7.0000000000000001?
After checking the information, we actually know that in JavsScript, when variables are stored There is no distinction between number and float types, but they are stored uniformly as float. JavaScript uses the 64-bit floating point format defined by the IEEE 754-2008 standard to store numbers. According to the definition of IEEE 754: http://en.wikipedia.org/wiki/IEEE_754-2008
The length of the integer corresponding to decimal64 is 10, The length of the decimal part is 16, so the default calculation result is "7.0000000000000001". If the last decimal is 0, take 1 as the valid digit flag.
Similarly, we can imagine that the result of 1/3 should be 0.3333333333333333.
So how to correct this value?
You can use the following methods:
1. parseInt
var r4=parseInt(i*100);
2. Math.round
var r2=Math.round((i*100)*1000)/1000;
Both of the above two methods can get 7
Attached is the full test code:
Copy code The code is as follows:
<html>
<head>
<title>Test script</title>
<script language="JAVASCRIPT">
function init()
{
var i = 0.07;
var r = i*100;
var r2=Math.round((i*100)*1000)/1000;
var r3 = eval(i*100);
var r4=parseInt(i*100) ;
var r5=parseFloat(i*100*1.0000);
var r6=(1/3);
alert(r);
alert("Math.round=" r2);
alert("eval=" r3);
alert("parseInt=" r4);
alert("parseFloat=" r5);
alert("" r6);
}
</script>
</head>
<body onload="init();">
</body>
</html>
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

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the values of float attribute?

What is the difference between float and double
