为什么230/100 * 100不返回230? [英] Why does 230/100*100 not return 230?

查看:170
本文介绍了为什么230/100 * 100不返回230?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

JavaScript的数学是否破碎?

在Javascript中,我无法弄清楚为什么 230/100 * 100 返回 229.99999999999997 ,而 240/100 * 100 返回 240。

In Javascript, I cannot figure out why 230/100*100 returns 229.99999999999997, while 240/100*100 returns 240.

这也适用于 460,920 依此类推......

This also applies to 460, 920 and so on...

有什么解决方案吗?

推荐答案

在JavaScript中,所有数值都存储为 IEEE 754 64位浮动 - 点值(在许多语言中也称为 double )。这种表示只有有限的精度(因此并非所有数字都能准确表示)而且它是二进制的,所以看起来很容易以十进制表示的值可能会产生问题。

In JavaScript all numeric values are stored as IEEE 754 64-bit floating-point values (also known as double in many languages). This representation has only finite precision (so not all numbers can be accurately represented) and it is binary, so values that seem to be easy to represent in decimal can turn out to be problematic to handle.

没有适用于所有人的即发即弃解决方案。如果您需要一个整数,那么只需使用 Math.round

There is no fire-and-forget solution suitable for everyone. If you need an integer then simply round using Math.round.

这篇关于为什么230/100 * 100不返回230?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆