事實上,0.1 + 0.2 這這樣的 0.1000000000000000055511151231257827021181583404541015625 + 0.200000000000000011102230246251565404236316680908203125 = 0.3000000000000000444089209850062616169452667236328125
這也是形成計算失精的主要緣由
解決方案:
若是是取整數,咱們能夠這樣:
Js代碼
- Math.round((0.07*100))
須要保留小數的,咱們能夠這樣:
Js代碼
- parseFloat((0.07*100).toPrecision(12)) // = 7
-
-
- parseFloat((0.01+0.02).toPrecision(12)) // = 0.03