乘法返回一个奇怪的结果[重复]

时间:2022-06-24 21:24:17

This question already has an answer here:

这个问题在这里已有答案:

I get the strangest result when I multiply 1.09 and 12. Actually 13.080000000000002 instead 13.08!!! What causes this? nd what is the solution

当我乘以1.09和12时,我得到了最奇怪的结果。实际上是13.080000000000002而不是13.08!是什么导致这个?什么是解决方案

console.log(1.09*12);

Regards.

1 个解决方案

#1


0  

As explained here:

如下所述:

In JavaScript all numbers are IEEE 754 floating point numbers. Due to the binary nature of their encoding, some decimal numbers cannot be represented with perfect accuracy. This is analagous to how the fraction 1/3 cannot be accurately represented with a decimal number with a finite number of digits. Once you hit the limit of your storage you'll need to round the last digit up or down.

在JavaScript中,所有数字都是IEEE 754浮点数。由于其编码的二进制特性,一些十进制数不能以完美的精度表示。这与分数1/3如何不能用具有有限位数的十进制数精确表示相似。达到存储限制后,您需要向上或向下舍入最后一位数字。

#1


0  

As explained here:

如下所述:

In JavaScript all numbers are IEEE 754 floating point numbers. Due to the binary nature of their encoding, some decimal numbers cannot be represented with perfect accuracy. This is analagous to how the fraction 1/3 cannot be accurately represented with a decimal number with a finite number of digits. Once you hit the limit of your storage you'll need to round the last digit up or down.

在JavaScript中,所有数字都是IEEE 754浮点数。由于其编码的二进制特性,一些十进制数不能以完美的精度表示。这与分数1/3如何不能用具有有限位数的十进制数精确表示相似。达到存储限制后,您需要向上或向下舍入最后一位数字。