当前位置:网站首页>Teach you a text to solve the problem of JS digital accuracy loss
Teach you a text to solve the problem of JS digital accuracy loss
2022-07-29 01:23:00 【Han Xu is working hard】
List of articles
One 、 About why we should solve the loss of accuracy
Let's look at an example , because js Loss of accuracy is also a common problem , Normally we can round or toFixed Keep decimals to solve 
Now the problem is that we know that the calculation result is equal to 0.01 But the final result is indeed true, If we encounter arithmetic problems , Decimal value comparison problem , Then we have to deal with him , Otherwise, there will also be an upper case , There is a logical judgment error
Two 、 How to solve js Loss of calculation accuracy ?
Normally, if it is Keep the decimal point 2 position 、3 Bits, etc , We can use common * Hundreds of digits 、 thousands After realizing the integer result Divide the result by the corresponding digit to achieve the result , as follows
console.log(5.22 - 5.21);

console.log((5.22 * 100 - 5.21 * 100) / 100);

According to the above description, let's look at our first example 
The result is what we want
3、 ... and 、toPrecision A specific method returns a rounded length string
Of course toFixed It can also achieve the corresponding length selection effect , Because the major browsers are aimed at toFixed All kinds of results are different. Interested students can refer to the following article
toFixed Detailed explanation
Here we only introduce toPrecision Refer to the method in detail 
Then we know what this method is used for
Of course, there is another point, that is, from left to right Not for 0 Start to calculate the position of
Example above Our result is 0.01
If we use toPrecision Words , Then the parameter to be filled in is 1

The result of the return is what we want 0.01
Then we match parseFloat After a floating-point numeric conversion of the string , Then compare and get the final result
console.log(5.22 - 5.21 < 0.01);
console.log((5.22 * 100 - 5.21 * 100) / 100 < 0.01);
var num = 5.22 - 5.21;
console.log(parseFloat(num.toPrecision(1)) < 0.01);

Conclusion
Create a little bit every day
Happy all day
Like, follow and collect
Beautiful day after day
Iron men Thank you for your support I need your third company 
边栏推荐
- 可视化全链路日志追踪
- ActiveMQ基本详解
- 一文让你搞懂MYSQL底层原理。-内部结构、索引、锁、集群
- uniapp movable-view表格缩放过程想保持容器高度不变的解决办法
- State compression DP Mondrian's dream
- 【Jenkins笔记】入门,自由空间;持续集成企业微信;allure报告,持续集成电子邮件通知;构建定时任务
- Textkit custom uilabel identification link
- system verilog常用语法
- Beginner's Guide to electronic bidding
- Hilbert transform and instantaneous frequency
猜你喜欢

北京护照西班牙语翻译推荐

如何处理项目中的时间、范围和成本限制?

Univariate function integration 1__ Indefinite integral

测试/开发程序员靠技术渡过中年危机?提升自己本身的价值......

Interviewer: programmer, please tell me who leaked the company interview questions to you?

进程和线程知识点总结 2

Day2:三种语言暴刷牛客130题

solidity实现智能合约教程(5)-NFT拍卖合约
![[unity] configure unity edit C as vscode](/img/f6/5675a96115fb97737c8b36a3fcc6ed.png)
[unity] configure unity edit C as vscode

电子招标初学者指南
随机推荐
[idea] where to use the query field
18 diagrams, intuitive understanding of neural networks, manifolds and topologies
量化交易之数字货币篇 - 生成foot print因子数据
C language bracket matching (stack bracket matching C language)
How to smoothly go online after MySQL table splitting?
New pseudo personal guide page source code
Seven SQL performance optimizations that spark 3.0 must know
Visual full link log tracking
Brushless DC motor controller (how much is the hall charge for changing the motor)
ActiveMQ basic details
DVWA之SQL注入
Flink Postgres CDC
Consumer unit
[notes for question brushing] delete continuous nodes with a total value of zero from the linked list
RHCE命令练习(一)
Mathematical modeling and detailed explanation of basic knowledge (common knowledge points of Chemistry)
[unity] configure unity edit C as vscode
Cookies and sessions
Charles -- teach you how to use the packet capturing tool from 0-1
一文搞懂ES6基本全部语法