《Mysql基础》【Mysql小数浮点数】double float decimal数据类型 编程入门 学习分享 【公开免费】

发布时间 2023-04-01 10:17:05作者: 刘贵庆

-- -- mysql数据库程序设计笔记:

-- -----------小数测试------------------
-- double浮点小数 (最多小数位后15位,) 使用8个字节存储。
-- float单精度小数:(最多小数位后6位)使用4个字节存储。
-- 举例保留2位:float(18,2),或:double(20,2)
-- decimal (最多小数位后30位)(存储空间更优,更小,以插入的数值大小为准)
-- (小数常用decimal类型)
-- 举例:decimal(20,2)

create table tb_float_test_1
(
id int not null auto_increment comment '自增id',
numone float(20,2) default null comment '单精度浮点小数值,保留2位',
numtwo double(22,2) default null comment '双精度浮点小数值,保留2位',
numthree decimal(22,2) default null comment '资金计算小数',
num decimal(22,0) default null comment '资金计算,取整',
primary key (id)
)engine=InnoDB default charset=utf8 comment='小数值测试表';

insert into tb_float_test_1(numone,numtwo,numthree,num) values('23.34','11.4340','42.23431','13.33');
insert into tb_float_test_1(numone,numtwo,numthree,num) values('213.323454','41.4350','742.23551','183.33453');
insert into tb_float_test_1(numone,numtwo,numthree,num) values('13.36624','431.4887340','12.24933431','155.3589675633');
insert into tb_float_test_1(numone,numtwo,numthree,num) values('133.36624','4531.4887340','162.24933431','1575.9589675633');


mysql> select * from tb_float_test_1;
+----+--------+---------+----------+------+
| id | numone | numtwo | numthree | num |
+----+--------+---------+----------+------+
| 4 | 23.34 | 11.43 | 42.23 | 13 |
| 5 | 213.32 | 41.44 | 742.24 | 183 |
| 6 | 13.37 | 431.49 | 12.25 | 155 |
| 7 | 133.37 | 4531.49 | 162.25 | 1576 |
+----+--------+---------+----------+------+
4 rows in set (0.00 sec)