关于MySQL的小数表示,浮点数与定点数

版权声明:本文原创,转载请注明出处。 https://blog.csdn.net/weixin_39004901/article/details/89330325

浮点数类型:float单精度,double双精度
定点数类型:decimal,默认decimal(10,0)

float占4bytes,double占8bytes,
而decimal的存储要求,整数和小数部分分别计算,每9位数字占用4bytes,而不足9位的,占用情况如下:
在这里插入图片描述
需要注意的是,如果小数位不够,采用四舍五入的方法存储,如:


mysql> desc test;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| a     | float(3,2)   | YES  |     | NULL    |       |
| b     | double(3,2)  | YES  |     | NULL    |       |
| c     | decimal(3,2) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into test values (1.236,1.236,1.236);
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> show warnings;
+-------+------+----------------------------------------+
| Level | Code | Message                                |
+-------+------+----------------------------------------+
| Note  | 1265 | Data truncated for column 'c' at row 1 |
+-------+------+----------------------------------------+
1 row in set (0.00 sec)

mysql> select * from test;
+------+------+------+
| a    | b    | c    |
+------+------+------+
| 1.24 | 1.24 | 1.24 |
+------+------+------+
1 row in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/weixin_39004901/article/details/89330325
今日推荐