当前位置:网站首页>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 
边栏推荐
- 【ManageEngine】助力哈尔滨工程大学实现网络流量一体化监控管理
- 【Jenkins笔记】入门,自由空间;持续集成企业微信;allure报告,持续集成电子邮件通知;构建定时任务
- Flink Postgres CDC
- 写作作业一
- Cookies and sessions
- Machine learning | matlab implementation of RBF radial basis function neural network Newrbe parameter setting
- TextKit 自定义UILabel识别链接
- 测试/开发程序员靠技术渡过中年危机?提升自己本身的价值......
- Google Play APK 上传其他国际应用商店
- 【mysql】多指标历史累计去重问题
猜你喜欢

mysql 创建索引的三种方式

Deep learning | matlab implementation of TCN time convolution neural network spatialdropoutlayer parameter description
![[unity] configure unity edit C as vscode](/img/f6/5675a96115fb97737c8b36a3fcc6ed.png)
[unity] configure unity edit C as vscode

Expression evaluation

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

【Jenkins笔记】入门,自由空间;持续集成企业微信;allure报告,持续集成电子邮件通知;构建定时任务

电子招标初学者指南

Univariate function integration 1__ Indefinite integral

Flask project architecture (First Edition

Flink SQL Hudi actual combat
随机推荐
Digital twin rail transit: "intelligent" monitoring to clear the pain points of urban operation
线程锁及锁的升降级
JS judge whether array / object array 1 contains array / object array 2
Canal real-time parsing MySQL binlog data practice
App access kakaotalk three party login
ActiveMQ basic details
How to create a custom 404 error page in WordPress
Oozie工作调度
指令重排、happens-before、as-if-serial
RHCE命令练习(一)
ACM SIGIR 2022 | 美团技术团队精选论文解读
Flask reports an error: pymysq1.err OperationalError:(1054, “Unknown column ‘None‘ in ‘field list‘“)
How to carry out engineering implementation of DDD Domain Driven Design
Synchronized keyword details
[Commons lang3 topic] 003- randomstringutils topic
How to deal with the time, scope and cost constraints in the project?
(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
量化交易之数字货币篇 - 生成foot print因子数据
Deep learning | matlab implementation of TCN time convolution neural network spatialdropoutlayer parameter description
写作作业一