请查看此查询
SELECT max( val_amd ) FROM `best_deposits`
表中的最大值等于 14.6(字段的类型为 float
),
但它返回14.3599996566772
为什么会发生这种情况,我怎样才能获得准确的值?
非常感谢
最佳答案
花车是邪恶的!
切勿使用 float 来存储金额或价格。相反,使用 int 并将金额存储为美分。这是永远解决这些问题的唯一方法。
为什么会发生这种情况:因为 floats在许多情况下无法准确保存(例如您的情况为0.6)
PS:到目前为止,我们对不同的语言已经有一百次这样的问题了:
- Use Float or Decimal for Accounting Application Dollar Amount?
- PHP rounding problem (5.2.3)?
- Rounding problem with double type
- Javascript rounding v c# rounding
- Python rounding problem
- ... and a lot more
编辑:您的评论:正如我所说:
use an int and store the amount in cents
(或者,您可以使用 DECIMAL(10,2) (或您需要多大/多少小数位)...不确定这是如何工作的)>
关于mysql 在处理 float 时,max 函数返回的不是精确值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4033115/