mysql数据类型-小数

  • decimal[(m,d])] [unsigned] [zerofill]

  • unsigned 表示无符号
  • zerofill 仅用于显示,当不满足时,按照左边补0

 


准确的小数值,m是数字总个数(负号不算),d是小数点后个数。 m最大值为65,d最大值为30。

例如:
create table L2(
	id int not null primary key auto_increment,
	salary decimal(8,2) 
)default charset=utf8;
mysql> create table L2(id int not null primary key auto_increment,salary decimal(8,2))default charset=utf8;
Query OK, 0 rows affected (0.03 sec)

mysql> insert into L2(salary) values(1.28);
Query OK, 1 row affected (0.01 sec)

mysql> insert into L2(salary) values(5.289);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> insert into L2(salary) values(5.282);  
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> insert into L2(salary) values(512132.28);
Query OK, 1 row affected (0.00 sec)

mysql> insert into L2(salary) values(512132.283);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from L2;
+----+-----------+
| id | salary    |
+----+-----------+
|  1 |      1.28 |
|  2 |      5.29 |
|  3 |      5.28 |
|  4 | 512132.28 |
|  5 | 512132.28 |
+----+-----------+
5 rows in set (0.00 sec)

mysql> insert into L2(salary) values(5121321.283); --这里插入数据时会报错,在创建表字段时,规定了总长度为8位(小数据点前面和后面共8位)
ERROR 1264 (22003): Out of range value for column 'salary' at row 1
mysql>

 

  • FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]

    单精度浮点数,非准确小数值,m是数字总个数,d是小数点后个数。
    
  • DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]

    双精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。
posted @ 2021-11-14 23:48  A熙  阅读(381)  评论(0编辑  收藏  举报