当前位置:网站首页>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 
边栏推荐
- How to smoothly go online after MySQL table splitting?
- Flink SQL Hudi 实战
- Self made | a 16 bit RISC architecture CPU is self-made by hand
- 进程和线程知识点总结 2
- DVWA之SQL注入
- [notes for question brushing] binary linked list to integer
- System Verilog common syntax
- 线程锁及锁的升降级
- 对接支付宝支付
- ThinkPHP high imitation blue cloud disk system program
猜你喜欢
随机推荐
(perfect solution) why is the effect of using train mode on the train/val/test dataset good, but it is all very poor in Eval mode
[Commons lang3 topic] 005- objectutils topic
如何处理项目中的时间、范围和成本限制?
【SQL之降龙十八掌】01——亢龙有悔:入门10题
转:认知亚文化
[Jenkins' notes] introduction, free space; Continuous integration of enterprise wechat; Allure reports, continuous integration of email notifications; Build scheduled tasks
18张图,直观理解神经网络、流形和拓扑
大页内存原理及使用设置
Log4j dynamic loading configuration file
Hilbert 变换与瞬时频率
日期转换 EEE MMM dd HH:mm:ss zzz yyyy
dart数组,Map,类型判断,条件判断运算符,类型转换
Univariate function integration 1__ Indefinite integral
uniapp createSelectorQuery().select 获取返回null报错
Flink SQL Hudi 实战
写作作业一
Textkit custom uilabel identification link
Google play APK uploads other international app stores
DVWA之SQL注入
Machine learning | matlab implementation of RBF radial basis function neural network Newrbe parameter setting









