添加十进制数时,parseFloat很奇怪

时间:2021-02-05 03:35:46

添加十进制数时,parseFloat很奇怪

Adding parseFloat(33.3) + 37.4 gives 70.699999999 in chrome console

添加parseFloat(33.3)+ 37.4在chrome控制台中给出了70.699999999

Can anyone explain me why this is happening and how to overcome this problem.

任何人都可以解释为什么会发生这种情况以及如何克服这个问题。

1 个解决方案

#1


0  

A quick solution is to round up to a specified number of decimals:

快速解决方案是舍入到指定的小数位数:

(33.3 + 37.4).toFixed(3); // would result in 70.700

#1


0  

A quick solution is to round up to a specified number of decimals:

快速解决方案是舍入到指定的小数位数:

(33.3 + 37.4).toFixed(3); // would result in 70.700