JS計算精度丟失

各大編程語言也都封裝了自己的精確計算庫,Js我選擇使用 math.js

安裝方式

1.包管理器安裝math.js

npm install mathjs

2.cdn

https://cdnjs.cloudflare.com/ajax/libs/mathjs/6.6.2/math.min.js

使用方式 

1.math.js 加、減、乘、除 

// 0.1+0.2
math.format(math.chain(math.bignumber(0.1)).add(math.bignumber(0.2)).done());
 
// 0.2-0.1
math.format(math.chain(math.bignumber(0.2)).subtract(math.bignumber(0.1)).done());
 
// 0.1*0.2
math.format(math.chain(math.bignumber(0.1)).multiply(math.bignumber(0.2)).done());
 
// 0.1/0.2
math.format(math.chain(math.bignumber(0.1)).divide(math.bignumber(0.2)).done());

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章