mysql float 浮点型

定点数类型 DEC等同于DECIMAL

浮点类型:FLOAT DOUBLE

 

作用:存储薪资、身高、体重、体质参数等

 

m,d 都是设置宽度

======================================
#FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]

定义:
        单精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。m最大值为255,d最大值为30

有符号:
           -3.402823466E+38 to -1.175494351E-38,
           1.175494351E-38 to 3.402823466E+38
无符号:
           1.175494351E-38 to 3.402823466E+38


精确度: 
           **** 随着小数的增多,精度变得不准确 ****


======================================
#DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]

定义:
           双精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。m最大值为255,d最大值为30

有符号:
           -1.7976931348623157E+308 to -2.2250738585072014E-308
           2.2250738585072014E-308 to 1.7976931348623157E+308

无符号:
           2.2250738585072014E-308 to 1.7976931348623157E+308

精确度:
           ****随着小数的增多,精度比float要高,但也会变得不准确 ****

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

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


精确度:
           **** 随着小数的增多,精度始终准确 ****
           对于精确数值计算时需要用此类型
           decaimal能够存储精确值的原因在于其内部按照字符串存储。

创建三张表 对三张表插入记录

mysql> create table t8(id float(255,30));
Query OK, 0 rows affected (0.01 sec)

mysql> create table t9(id double(255,30));
Query OK, 0 rows affected (0.01 sec)

mysql> 
mysql> create table t10(id decimal(65,30));
Query OK, 0 rows affected (0.02 sec)

mysql> 
mysql> insert into t8 values(1.1111111111111111111111111111111111111111111111);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t9 values(1.1111111111111111111111111111111111111111111111);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t10 values(1.1111111111111111111111111111111111111111111111);
Query OK, 1 row affected, 1 warning (0.00 sec)

三张表比较一下 精确度

float #随着小数的增多,精度开始不准确
mysql> select * from t8;
+----------------------------------+
| id                               |
+----------------------------------+
| 1.111111164093017600000000000000 |
+----------------------------------+
1 row in set (0.00 sec)
 DOUBLE #精度比float要准确点,但随着小数的增多,同样变得不准确
mysql> select * from t9;
+----------------------------------+
| id                               |
+----------------------------------+
| 1.111111111111111200000000000000 |
+----------------------------------+
1 row in set (0.00 sec)
decimal #精度始终准确,d为30,于是只留了30位小数


mysql> select * from t10;
+----------------------------------+
| id                               |
+----------------------------------+
| 1.111111111111111111111111111111 |
+----------------------------------+
1 row in set (0.00 sec)

原文地址:https://www.cnblogs.com/mingerlcm/p/9799155.html