decimal 的解释说明

官方的定义:
DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL]

A packed “exact” fixed-point number. M is the total number of digits (the precision) and D is the
number of digits after the decimal point (the scale). The decimal point and (for negative numbers)
the - sign are not counted in M. If D is 0, values have no decimal point or fractional part.
The maximum number of digits (M) for DECIMAL is 65. The maximum number of supported decimals (D)
is 30. If D is omitted, the default is 0. If M is omitted, the default is 10.
UNSIGNED, if specified, disallows negative values.
All basic calculations (+, -, *, /) with DECIMAL columns are done with a precision of 65 digits.
–示例:
mysql> create table t1(c1 decimal,c2 decimal(65),c3 decimal(30,30),c4 decimal(65,30));
Query OK, 0 rows affected (0.01 sec)
mysql> desc t1;
±------±---------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------±---------------±-----±----±--------±------+
| c1 | decimal(10,0) | YES | | NULL | |
| c2 | decimal(65,0) | YES | | NULL | |
| c3 | decimal(30,30) | YES | | NULL | |
| c4 | decimal(65,30) | YES | | NULL | |
±------±---------------±-----±----±--------±------+
4 rows in set (0.00 sec)
decimal:数字型,不存在精度损失,常用于银行帐目计算
decimal(P,m)
P的取值范围是1–65,
M的取值是0–30.
精度M最大为30,表示有30位小数。
decimal 整数和小数位均不写 默认为decimal(10,0)
若小数位不写默认的小数位为0.
–做极限值测试插入:
mysql> insert into t1(c1,c2,c3,c4)select repeat(9,10),repeat(9,65),1/POWER(10,30),CONCAT(REPEAT(9,35),1/POWER(10,30));
Query OK, 1 row affected, 2 warnings (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 2

mysql> select * from t1\G
*************************** 1. row ***************************
c1: 9999999999
c2: 99999999999999999999999999999999999999999999999999999999999999999
c3: 0.000000000000000000000000000001
c4: 100000.000000000000000000000000000000
1 row in set (0.00 sec)
mysql> select length(c1),length(c2),length(c3),length(c4) from t1;
±-----------±-----------±-----------±-----------+
| length(c1) | length(c2) | length(c3) | length(c4) |
±-----------±-----------±-----------±-----------+
| 10 | 65 | 32 | 37 |
±-----------±-----------±-----------±-----------+
1 row in set (0.00 sec)
在实际中常用的类型为:
decimal(10,7) 用于存储经纬度 ,可以精确到米。
decimal(18,2) 存储的金额为9999千亿,存储整数位16个
若存储的小数位比较多可以扩展整数位 decimal(22,6) 整数位16个小数位6个。
在产品设计的时候可以约定。
decimal 还可以存储超过bigint的整数位,最大存储65位的整数类型。
还可以存储对应位数的负数:
mysql> insert into t1(c1) values(-9999999999);
Query OK, 1 row affected (0.00 sec)

————————————————
版权声明:本文为CSDN博主「vkingnew」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/vkingnew/article/details/89498353

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值