在我的数据库中,一些字段(金额、余额)被分配为 float 值,但现在我遇到了问题。如果金额是 1.56,则需要 1.6 然后我使用 Round(amount,2) 更改表格 仍然显示出同样的问题。 如果有人知道请帮助我。
最佳答案
浮点字段必须声明为 float(some_integer, 1) 以显示您所描述的行为。此类声明中的第一个整数告诉MySQL总共应该有多少位数字可见,小数点左边的数字+小数点右边的数字。要让小数点左边 3 位、右边 2 位,您可以将其声明为 float(5,2)
。
就像 Doan Cuong 已经提到的那样,如果您选择十进制数据类型会更好。
查看它的不同行为 live here .
引用手册:
The DECIMAL and NUMERIC types store exact numeric data values. These types are used when it is important to preserve exact precision, for example with monetary data.
The FLOAT and DOUBLE types represent approximate numeric data values.
有关数据类型的更多信息,请阅读更多 here .
关于mysql - 数据库中的浮点值转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15828279/