MySQL回合怪异的bug [英] MySQL round weird bug
问题描述
我正面临着一个非常奇怪的错误?在mysql + php上. 是一个简单的选择,在下面的示例中,我使用多个字段来尝试解释我的问题:
I'm facing a really weird ¿bug? on mysql+php right now. Is a simple select, in the following example i'm using multiple fields to try to explain my problem:
- 字段"为11.5
- $ phpvar为1.15
MySQL查询:
select round(field * " . $phpvar . " ,2) as a1,
round(field * 1.15 ,2) as a2,
round(11.5 * " . $phpvar . " ,2) as a3,
round(11.5 * 1.15 ,2) as a4,
field * " . $phpvar . " as a5
from ...
好的,我正在尝试获得13.23. "field" * $phpvar = 13.225
,所以使用round(13.225,2)我应该得到13.23,对吧?好,是的,不是.
ok, i'm trying to get 13.23. "field" * $phpvar = 13.225
, so using round(13.225,2) I should get 13.23, right? well, yes and no.
查询结果:
- a1 [round(field *.$ phpvar.",2)] => 13.22
- a2 [round(field * 1.15,2)] => 13.22
- a3 [round(11.5 *.$ phpvar.",2)] => 13.23
- a4 [round(11.5 * 1.15,2)] => 13.23
- a5 [field *.$ phpvar."] => 13.225(无回合)
我想念什么?在使用字段"时,我的结果怎么可能会假一轮?
what am I missing? how is it possible, when it comes to use "field", my result gets a fake round?
推荐答案
问题是如何存储DOUBLE和FLOAT值.
The problem is how the DOUBLE and FLOAT values are stored.
可能(也可能)将11.5或22.475 coul的值存储在近似值(如11.499999999999〜或22.475000000000000001)中,因此某些计算或四舍五入可能导致错误的结果.
It is possible (and probable) that values like 11.5 or 22.475 coul be stored in approximated values like 11.499999999999~ or 22.475000000000000001 thus some calculations or roundings could lead to incorrect results.
始终最好将浮点值存储为 DECIMAL 列明类型,在该类型中,该值应与所有十进制数字完全存储在一起,而不是近似值.
It is always better to store float values into a DECIMAL coulmn type where the value is stored exactly with all the decimal digits and is not approximated.
这篇关于MySQL回合怪异的bug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!