浮点列与定点列
float(M,D),m是精度,总位数,D标度,小数点后面的位数
例如:float(5,2)
double
decimal
float,double, 有精度损失
decimal 定点型,更精确
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| a | float(5,2) | YES | | NULL | |
| b | double | YES | | NULL | |
| c | float(9,2) | YES | | NULL | |
| d | decimal(9,2) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
//insert into t3 values(512.11,1234567.59,1234567.23,1234567.23);
--select * from t3;
+--------+------------+------------+------------+
| a | b | c | d |
+--------+------------+------------+------------+
| 999.99 | 1234567.23 | NULL | NULL |
| 999.99 | 1234567.23 | 1234567.25 | NULL |
| 512.11 | 1234567.59 | 1234567.25 | 1234567.23 |
float(M,D),m是精度,总位数,D标度,小数点后面的位数
例如:float(5,2)
double
decimal
float,double, 有精度损失
decimal 定点型,更精确
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| a | float(5,2) | YES | | NULL | |
| b | double | YES | | NULL | |
| c | float(9,2) | YES | | NULL | |
| d | decimal(9,2) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
//insert into t3 values(512.11,1234567.59,1234567.23,1234567.23);
--select * from t3;
+--------+------------+------------+------------+
| a | b | c | d |
+--------+------------+------------+------------+
| 999.99 | 1234567.23 | NULL | NULL |
| 999.99 | 1234567.23 | 1234567.25 | NULL |
| 512.11 | 1234567.59 | 1234567.25 | 1234567.23 |
+--------+------------+------------+------------+
可以看出,全部精度设置为9位,但是小数后面有细小误差,
decimal以及double都要比float精确度要好一些,推荐使用
本文通过实例比较了浮点数类型float与定点数类型decimal在不同精度设置下的表现,展示了decimal相较于float和double拥有更高的精确度,推荐在需要高精度计算的场景下使用decimal。

862

被折叠的 条评论
为什么被折叠?



