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 Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the maximum value of float? What is the maximum value of float? Oct 11, 2023 pm 05:54 PM

What is the maximum value of float?

What are the database float lengths? What are the database float lengths? Oct 10, 2023 pm 03:57 PM

What are the database float lengths?

What is the accuracy of float? What is the accuracy of float? Oct 17, 2023 pm 03:13 PM

What is the accuracy of float?

What does float mean in c language? What does float mean in c language? Oct 12, 2023 pm 02:30 PM

What does float mean in c language?

How to convert string to float How to convert string to float Oct 16, 2023 pm 02:03 PM

How to convert string to float

What are the values ​​of float attribute? What are the values ​​of float attribute? Oct 10, 2023 pm 02:03 PM

What are the values ​​of float attribute?

What is the difference between float and double What is the difference between float and double Oct 11, 2023 pm 05:38 PM

What is the difference between float and double

What does float=0 mean? What does float=0 mean? Oct 13, 2023 pm 04:14 PM

What does float=0 mean?

See all articles