问题描述
我正在存储货币值,并且我读到FLOAT存在内部舍入问题(尽管我不能说我从未注意到任何问题),并且最好使用DECIMAL.
I am storing monetary values, and I read that FLOAT has internal rounding problems (although I can't say I ever noticed any problems) and that it is better to use DECIMAL.
所以我一直在使用DECIMAL,但是当我尝试将值设置为1时,它存储为0.99.我将在某个时候在JavaScript中使用该值,并且我知道我的JS计算将是错误的,因为它是0.99而不是1.00.
So I have been using DECIMAL but when I try to set the value to 1 it is stored as 0.99. I will be using that value in JavaScript at some point, and I know that my JS calculations will be wrong because it is 0.99 not 1.00.
为什么这样做,我应该只使用FLOAT吗?
Why is it doing that and should I just use FLOAT?
推荐答案
根据事物的外观,您需要DECIMAL(4, 2)
. DECIMAL(2, 2)
仅允许范围-0.99 to 0.99
You need DECIMAL(4, 2)
by the looks of things. DECIMAL(2, 2)
only allows a range of -0.99 to 0.99
精度代表为以下内容存储的有效位数值,小数位代表小数点后可以存储的位数.因此,在您的情况下,您尚未定义该列以允许任何整数部分.
The precision represents the number of significant digits that are stored for values, and the scale represents the number of digits that can be stored following the decimal point. so in your case you have not defined the column to allow any integer part at all.
这篇关于MySQL-DECIMAL(2,2)中的1值为0.99的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!