程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> MySQL浮點型表示

MySQL浮點型表示

編輯:MySQL綜合教程

MySQL浮點型表示


MySQL浮點型和定點型可以用類型名稱後加(M,D)來表示,M表示該值的總共長度,D表示小數點後面的長度,M和D又稱為精度和標度,如float(7,4)的可顯示為-999.9999,MySQL保存值時進行四捨五入,如果插入999.00009,則結果為999.0001。FLOAT和DOUBLE在不指定精度時,默認會按照實際的精度來顯示,而DECIMAL在不指定精度時,默認整數為10,小數為0。

創建下表:
mysql> CREATE TABLE t2(id1 FLOAT(5,2) DEFAULT NULL,id2 DOUBLE(5,2) DEFAULT NULL,id3 DECIMAL(5,2) DEFAULT NULL);

mysql> DESC t2;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id1 | float(5,2) | YES | | NULL | |
| id2 | double(5,2) | YES | | NULL | |
| id3 | decimal(5,2) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+

往id1,id2,id3這三個字段中插入數據1.23:

mysql> INSERT INTO t2(id1,id2,id3) VALUES(1.23,1.23,1.23);

mysql> SELECT * FROM t2;
+------+------+------+
| id1 | id2 | id3 |
+------+------+------+
| 1.23 | 1.23 | 1.23 |
+------+------+------+

數據都正確插入,再向id1插入1.234,id2插入1.234,id3仍然插入1.23:
mysql> INSERT INTO t2(id1,id2,id3) VALUES(1.234,1.234,1.23);

mysql> SELECT * FROM t2;
+------+------+------+
| id1 | id2 | id3 |
+------+------+------+
| 1.23 | 1.23 | 1.23 |
| 1.23 | 1.23 | 1.23 |
+------+------+------+

數據全部正確插入,但是id1和id2由於標度的限制,捨去了最後一位。

同時向id1,id2,id3中插入數據1.234:

mysql> INSERT INTO t2(id1,id2,id3) VALUES(1.234,1.234,1.234);
Query OK, 1 row affected, 1 warning (0.02 sec)
mysql> SELECT * FROM t2;
+------+------+------+
| id1 | id2 | id3 |
+------+------+------+
| 1.23 | 1.23 | 1.23 |
| 1.23 | 1.23 | 1.23 |
| 1.23 | 1.23 | 1.23 |
+------+------+------+
3 rows in set (0.00 sec)

數據也插入成功,但是有一個錯誤提示,

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

將id1,id2,id3的精度和標度都去掉,再插入數據1.234:

mysql> ALTER TABLE t2 MODIFY id1 FLOAT;
Query OK, 0 rows affected (0.14 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> ALTER TABLE t2 MODIFY id2 DOUBLE;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> ALTER TABLE t2 MODIFY id3 DECIMAL;
Query OK, 4 rows affected, 4 warnings (0.06 sec)
Records: 4 Duplicates: 0 Warnings: 4

mysql> DESC t2;
+-------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| id1 | float | YES | | NULL | |
| id2 | double | YES | | NULL | |
| id3 | decimal(10,0) | YES | | NULL | |
+-------+---------------+------+-----+---------+-------+

mysql> INSERT INTO t2(id1,id2,id3) VALUES(1.234,1.234,1.234);
Query OK, 1 row affected, 1 warning (0.00 sec)

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

mysql> SELECT * FROM t2;
+-------+-------+------+
| id1 | id2 | id3 |
+-------+-------+------+
| 1.234 | 1.234 | 1 |
+-------+-------+------+
1 row in set (0.00 sec)

id1和id2的數據正確插入,而id3被截斷。

浮點數如果不寫精度和標度,則會按照實際顯示,如果有精度和標度,則會將數據四捨五入後插入,系統不報錯,定點數如果不設置精度和標度,剛按照默認的(10,0)進行操作,如果數據超過了精度和標度值,則會報錯。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved