DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL]
A packed “exact” fixed-point number.
M
is the total number of digits (the precision) andD
is the number of digits after the decimal point (the scale). The decimal point and (for negative numbers) the-
sign are not counted inM
. IfD
is 0, values have no decimal point or fractional part. The maximum number of digits (M
) forDECIMAL
is 65. The maximum number of supported decimals (D
) is 30. IfD
is omitted, the default is 0. IfM
is omitted, the default is 10. (There is also a limit on how long the text ofDECIMAL
literals can be; see Section 12.22.3, “Expression Handling”.)
UNSIGNED
, if specified, disallows negative values.All basic calculations (
+, -, *, /
) withDECIMAL
columns are done with a precision of 65 digits.
- M是总位数(精度),D是小数点后的位数.
- 小数点和符号(+-) ,不计入M
- 如果D为0,则值没有小数点或小数部分
- M最大位数为65,D最大位数为30
- 如果忽略M,默认为10
- 如果忽略D,默认为0
- 如果指定了UNSIGNED,则不允许使用负值。
- 使用DECIMAL列的所有基本计算(+,-,*,/)均以65位精度完成。
DECIMAL同义词
DEC[(M[,D])] [UNSIGNED] [ZEROFILL], NUMERIC[(M[,D])] [UNSIGNED] [ZEROFILL], FIXED[(M[,D])] [UNSIGNED] [ZEROFILL]
These types are synonyms for DECIMAL. The FIXED synonym is available for compatibility with other database systems.
例子
create table decimal_table(id serial, m1 decimal(20,8),m2 dec(10,2),m3 NUMERIC(8,2),m4 fixed(6,2),un2 decimal(10,2) unsigned,zf2 decimal(10,2) zerofill)
从这图可知,最终都变成了decimal
举例总结
本文例子在MySql5.7.23中测试
以decimal(10,2)为例
# ok 最终保存的是99999999.99
insert decimal_table(un2) values(000099999999.99499999999999999999999)
# fail
insert decimal_table(un2) values(000099999999.995)
# fail zf2是无符号,如果是有符号就不会报错
insert decimal_table(zf2) values(-0.01)
- 最大值是99999999.99
- 整数部分以非0开头的数字,最多(10-2)=8位,比如000099999999,这个算8位
- 小数部分,最终保存是2位小数(四舍五入)
- 如果指定了UNSIGNED或ZEROFILL,则不允许使用负值
- 一句话最终四舍五入保留2位小数点后,超过99999999.99,就会报错
FLOAT(p
) [UNSIGNED] [ZEROFILL]
DOUBLE[(M
,D
)] [UNSIGNED] [ZEROFILL]