


Reason: The integer type will be automatically converted into an integer type for calculation, and the decimal type will be directly converted into a double type for calculation; and the double type needs to be accurate to 15 digits after the decimal point, so the JavaScript subtraction of decimals will result in a long string of decimal places.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
The reason why a long string of decimal places appears when subtracting decimals in javascript
<script> var a='38.8'; var b='6.8'; alert(parseFloat(a)-parseFloat(b)); var a=134.22; var b=6; alert(a*b); </script>
Why does the above code produce a long string of decimal places? Although it is more accurate, it can It's not necessary.
This is related to the data structure. The integer type is automatically converted into a positive type for calculation, and the decimal type is directly converted into a double type for calculation. This is necessary when computing in memory. You should know that the computer only recognizes 0 and 1. The specific problem is the accuracy of floating point.
float is accurate to 7 decimal places
double is accurate to 15 decimal places
javascript:document.write( (11.3-10.1).toFixed(2) )## The #toFixed() method not only truncates excess decimal places, it also rounds based on the next decimal place at the interception position. For example, for the value 10.739, truncated to two digits after the decimal point, the result would be 10.74. For the value 10.732, truncated to two digits after the decimal point, the result will be 10.73. Note that in JavaScript we can only intercept decimals from 0 to 20 digits after the decimal point. The toFixed() method is only supported by higher versions of browsers, so it is best to check whether the browser supports this method before using it. The check code is as follows:
var varNumber = 22.234; if (varNumber.toFixed) { varNumber = varNumber.toFixed(2); } else //浏览器不支持toFixed()就使用其他方法 { var div = Math.pow(10,2); varNumber = Math.round(varNumber * div) / div; }This can be solved, but you want to ask how it is possible to have so many decimal points.
Why is there such an incomprehensible answer?
I Googled it and found out that this is a bug in JavaScript floating point operations. For example: 7*0.8 JavaScript calculates it as: 5.6000000000000005I found some solutions online, which is to rewrite some floating point operations functions. These methods are excerpted below for reference by friends who encounter the same problem: Program code//除法函数,用来得到精确的除法结果 //说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。 //调用:accDiv(arg1,arg2) //返回值:arg1除以arg2的精确结果 function accDiv(arg1,arg2){ var t1=0,t2=0,r1,r2; try{t1=arg1.toString().split(".")[1].length}catch(e){} try{t2=arg2.toString().split(".")[1].length}catch(e){} with(Math){ r1=Number(arg1.toString().replace(".","")) r2=Number(arg2.toString().replace(".","")) return (r1/r2)*pow(10,t2-t1); } } //给Number类型增加一个div方法,调用起来更加方便。 Number.prototype.div = function (arg){ return accDiv(this, arg); } //乘法函数,用来得到精确的乘法结果 //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。 //调用:accMul(arg1,arg2) //返回值:arg1乘以arg2的精确结果 function accMul(arg1,arg2) { var m=0,s1=arg1.toString(),s2=arg2.toString(); try{m+=s1.split(".")[1].length}catch(e){} try{m+=s2.split(".")[1].length}catch(e){} return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m) } //给Number类型增加一个mul方法,调用起来更加方便。 Number.prototype.mul = function (arg){ return accMul(arg, this); } //加法函数,用来得到精确的加法结果 //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。 //调用:accAdd(arg1,arg2) //返回值:arg1加上arg2的精确结果 function accAdd(arg1,arg2){ var r1,r2,m; try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0} try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0} m=Math.pow(10,Math.max(r1,r2)) return (arg1*m+arg2*m)/m } //给Number类型增加一个add方法,调用起来更加方便。 Number.prototype.add = function (arg){ return accAdd(arg,this); }Include these functions where you want to use them, and then Just call it to calculate. For example, if you want to calculate: 7*0.8, change it to (7).mul(8)Other operations are similar, and you can get more accurate results. [Related recommendations:
The above is the detailed content of Why does javascript decimal subtraction have a long list of decimal places?. For more information, please follow other related articles on the PHP Chinese website!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

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

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