当前位置:网站首页>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 
边栏推荐
- Connect with Alipay payment
- 正则校验与时间格式化
- (update 20211130) about the download and installation of Jupiter notebook and its own configuration and theme
- C语言300行代码实现扫雷(可展开+可标记+可更改困难级别)
- solidity实现智能合约教程(5)-NFT拍卖合约
- regular expression
- Canal实时解析mysql binlog数据实战
- [Commons lang3 topic] 004- numberutils topic
- Common functions and usage of numpy
- Recursion and divide and conquer
猜你喜欢

大页内存原理及使用设置
![[idea] where to use the query field](/img/63/f95868907364fc949885c67c34ba32.png)
[idea] where to use the query field

Flask project architecture (First Edition
![[Jenkins' notes] introduction, free space; Continuous integration of enterprise wechat; Allure reports, continuous integration of email notifications; Build scheduled tasks](/img/0f/f75603616bd54039f8ccfa3e024768.png)
[Jenkins' notes] introduction, free space; Continuous integration of enterprise wechat; Allure reports, continuous integration of email notifications; Build scheduled tasks

How to implement the time impact analysis of the construction project?

18 diagrams, intuitive understanding of neural networks, manifolds and topologies

mysql分表之后怎么平滑上线?
![[web development] basic knowledge of flask framework](/img/79/5ece84552c82e98f5e15fac1ee3335.png)
[web development] basic knowledge of flask framework

进程和线程知识点总结1

This article enables you to understand the underlying principle of MySQL- Internal structure, index, lock, cluster
随机推荐
Instruction rearrangement, happens before, as if serial
Common functions and usage of numpy
Summary of process and thread knowledge points 2
Hilbert 变换与瞬时频率
递归与分治
[Commons lang3 topic] 004- numberutils topic
LeTax记录\documentclass{},authoryear属性使用
How to create a custom 404 error page in WordPress
Linux redis source code installation
括号匹配的检验
Deep learning | matlab implementation of TCN time convolution neural network spatialdropoutlayer parameter description
[Commons lang3 topic] 003- randomstringutils topic
ActiveMQ basic details
[notes for question brushing] binary linked list to integer
Synchronized keyword details
Interviewer: programmer, please tell me who leaked the company interview questions to you?
Log4j dynamic loading configuration file
Connect with Alipay payment
Spark 3.0 中七个必须知道的 SQL 性能优化
数字孪生轨道交通:“智慧化”监控疏通城市运行痛点