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.
我存储货币价值,我读到FLOAT有内部舍入问题(虽然我不能说我曾经注意到任何问题)并且最好使用DECIMAL。
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.
所以我一直在使用DECIMAL但是当我尝试将值设置为1时,它存储为0.99。我将在某些时候在JavaScript中使用该值,并且我知道我的JS计算将是错误的,因为它是0.99而不是1.00。
Why is it doing that and should I just use FLOAT?
为什么这样做,我应该只使用FLOAT?
1 个解决方案
#1
18
You need DECIMAL(4, 2)
by the looks of things. DECIMAL(2, 2)
only allows a range of -0.99 to 0.99
根据事物的外观,你需要DECIMAL(4,2)。 DECIMAL(2,2)仅允许-0.99到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.
精度表示为值存储的有效位数,刻度表示小数点后可存储的位数。所以在你的情况下你没有定义列以允许任何整数部分。
#1
18
You need DECIMAL(4, 2)
by the looks of things. DECIMAL(2, 2)
only allows a range of -0.99 to 0.99
根据事物的外观,你需要DECIMAL(4,2)。 DECIMAL(2,2)仅允许-0.99到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.
精度表示为值存储的有效位数,刻度表示小数点后可存储的位数。所以在你的情况下你没有定义列以允许任何整数部分。