MySQL多次遇到小数这个难点

encountered many times this difficulty of decimal in MySQL

我在MySQL!

中多次遇到这个小数问题

当我输入这个类型时:DECIMAL(10,8)

允许的最大值为:99.99999999 !

应该是:9999999999.99999999不?

我要小数点(.)后8位的最大值。

来自documentation

The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.7 are as follows:

  • M is the maximum number of digits (the precision). It has a range of 1 to 65.
  • D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.

第一个值不是小数点左边的位数,而是总位数。

这就是为什么值 9999999999.99999999DECIMAL(10, 8) 不可能的原因:它有 18 位数字长。

第一个数是位数的总和,第二个是小数位数。

对于您请求的号码,请尝试 DECIMAL(18,8)

DECIMAL(x,y) 说明符的工作方式是 x 代表总位数,y 代表小数点后的数字。

10,8 表示 NN.NNNNNNNN.

如果你想要更多,你需要相应地扩大你的范围。

一个decimal定义了两个参数——DECIMAL(M, D),其中M是总位数,D是小数点后的位数M。要正确表示数字 9999999999.99999999,您需要使用 DECIMAL(18, 8).

你定义DECIMAL(total positions, total decimal)所以记住:total decimal使用total positions的positions,如果你想要最左边和右边的数字,使用FLOAT类型。