当前位置:网站首页>教你一文解决 js 数字精度丢失问题
教你一文解决 js 数字精度丢失问题
2022-07-28 23:56:00 【韩旭在努力】
一、关于为什么要解决精度丢失
可以看下例子,因为js失去精度问题也是常见的问题,正常我们可以四舍五入或者 toFixed保留小数这种去解决
现在遇到问题是我们明知道计算结果是等于0.01的但是最后的结果确实true,如果我们遇到运算问题,小数数值比对问题,那么我们就必须要去解决他,否则也就会出现上者情况,出现逻辑判断出错问题
二、怎么解决js的计算精度丢失问题?
正常来说如果是 小数点保留后2位、3位等等,我们可以使用常见的 * 百位数、千位数 实现整位结果后 将结果在除以对应的数位实现结果,如下
console.log(5.22 - 5.21);

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

根据上方的描述我们再去着眼看我们最开始的例子
结果也就成了我们想要的结果
三、toPrecision 特定方法返回四舍五入长度字符串
当然toFixed也是可以实现对应的长度取舍效果的,因为各大浏览器针对toFixed的各类结果都是不同的有兴趣的同学可以参考下下面这篇文章
toFixed详解
在这里我们只介绍 toPrecision 参照方法详解
那么我们也就知道了此方法是用来干什么的
当然再有一点也就是从左到右 不为0的位置开始计算
上方举例 我们的结果是0.01
如果我们去使用toPrecision的话,那么要填写的参数则为1

返还的结果也就是我们想要的 0.01
然后我们搭配 parseFloat 对字符串进行一个浮点数值转换后,然后对比得出最后结果
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);

结语
每天创作一点点
开心快乐一整天
点赞关注加收藏
美好一天又一天
铁铁们 感谢支持 我需要你们的三连 
边栏推荐
- Y80. Chapter 4 Prometheus big factory monitoring system and practice -- Kube state metrics component introduction and monitoring extension (XI)
- Necessary interview skills for Android (including interview questions and learning materials)
- 追踪伦敦银实时行情的方法
- 【树莓派】widows电脑如何与树莓派连接
- 面试突击69:TCP 可靠吗?为什么?
- 【Jenkins笔记】入门,自由空间;持续集成企业微信;allure报告,持续集成电子邮件通知;构建定时任务
- Main thread and daemon thread
- [Commons lang3 topic] 003- randomstringutils topic
- PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益
- 线程锁及锁的升降级
猜你喜欢

AQS principle

Summary of preprocessing methods for time series data

【AD学习】本次海上航行器大赛画pcb图的历程

Visual full link log tracking

图扑软件亮相 2022 福州数博会,携手共创数字新时代
![[target detection] Introduction to yolor theory + practical test visdrone data set](/img/cd/3cb13d6d79cd207c6d637af7756ffc.png)
[target detection] Introduction to yolor theory + practical test visdrone data set
![[unity] configure unity edit C as vscode](/img/f6/5675a96115fb97737c8b36a3fcc6ed.png)
[unity] configure unity edit C as vscode

DDD领域驱动设计如何进行工程化落地

Charles -- 从0-1教你如何使用抓包工具
![“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“](/img/f2/37a1e65eb1104d72128f96fc5d9c85.png)
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“
随机推荐
Mathematical modeling and detailed explanation of basic knowledge (common knowledge points of Chemistry)
ThinkPHP高仿蓝奏云网盘系统程序
dart数组,Map,类型判断,条件判断运算符,类型转换
Wechat campus bathroom reservation applet graduation design finished product (8) graduation design thesis template
追踪伦敦银实时行情的方法
【刷题笔记】从链表中删去总和值为零的连续节点
Wechat campus bathroom reservation for the finished product of applet graduation design (7) mid term inspection report
solidity实现智能合约教程(5)-NFT拍卖合约
[raspberry pie] how does the windows computer connect with raspberry pie
【commons-lang3专题】004- NumberUtils 专题
SQL Server 只有数据库文件,没有日志文件,恢复数据时报1813错误的解决方案
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(7)中期检查报告
Consumer unit 消费单元
Brushless DC motor controller (how much is the hall charge for changing the motor)
Seven marketing strategies of NFT project
【刷题笔记】二进制链表转整数
[Jenkins' notes] introduction, free space; Continuous integration of enterprise wechat; Allure reports, continuous integration of email notifications; Build scheduled tasks
The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!
[AD learning] the course of PCB drawing in this marine vehicle competition
Time series prediction | MATLAB realizes time series prediction of TCN time convolution neural network