mysql float double 類型

1.float類型
float列類型默認長度查不到結果。必須指定精度。
比方 num float, insert into table (num) values (0.12); select * from table where num=0.12的話。empty set。
num float(9,7), insert into table (num) values (0.12); select * from table where num=0.12的話會查到這條記錄。mysql

mysql> create table tt
-> (
-> num float(9,3)
-> );
Query OK, 0 rows affected (0.03 sec)sql

mysql> insert into tt(num)values(1234567.8);
ERROR 1264 (22003): Out of range value for column 'num' at row 1
注:超出字段範圍,沒法插入spa

mysql> insert into tt(num)values(123456.8);
Query OK, 1 row affected (0.00 sec)table

mysql> select * from tt;
+------------+
| num |
+------------+
| 123456.797 |
+------------+
1 row in set (0.00 sec)
注:小數位數不夠,本身主動補齊,但是存在一個問題就是如上的近似值。class

mysql> insert into tt(num)values(123456.867);
Query OK, 1 row affected (0.04 sec)select

mysql> select * from tt;
+------------+
| num |
+------------+
| 123456.797 |
| 123456.797 |
| 123456.867 |
+------------+
3 rows in set (0.00 sec)float

mysql> select * from tt where num=123456.867;
+------------+
| num |
+------------+
| 123456.867 |
+------------+
1 row in set (0.00 sec)tab

mysql> insert into tt(num)values(2.8);
Query OK, 1 row affected (0.04 sec)di

mysql> select * from tt;
+------------+
| num |
+------------+
| 123456.797 |
| 123456.797 |
| 123456.867 |
| 2.800 |
+------------+
4 rows in set (0.00 sec)co

mysql> select * from tt where num=2.8;
+-------+
| num |
+-------+
| 2.800 |
+-------+
1 row in set (0.00 sec)

mysql> insert into tt(num)values(2.888888);
Query OK, 1 row affected (0.00 sec)

mysql> select * from tt;
+------------+
| num |
+------------+
| 123456.797 |
| 123456.797 |
| 123456.867 |
| 2.800 |
| 2.889 |
+------------+
5 rows in set (0.00 sec)
注:小數位數超了,本身主動取近似值。

--------------------------------------------------------------------------------------

2.double類型

mysql> create table tt(
-> num double(9,3)
-> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into tt(num) values(234563.9);
Query OK, 1 row affected (0.00 sec)

mysql> select * from tt;
+------------+
| num |
+------------+
| 234563.900 |
+------------+
1 row in set (0.00 sec)

mysql> insert into tt(num) values(2345623.2);
ERROR 1264 (22003): Out of range value for column 'num' at row 1
mysql> insert into tt(num) values(234563.2);
Query OK, 1 row affected (0.00 sec)

mysql> select * from tt;
+------------+
| num |
+------------+
| 234563.900 |
| 234563.200 |
+------------+
2 rows in set (0.00 sec)

mysql> insert into tt(num) values(2.8);
Query OK, 1 row affected (0.00 sec)

mysql> select * from tt;
+------------+
| num |
+------------+
| 234563.900 |
| 234563.200 |
| 2.800 |
+------------+
3 rows in set (0.00 sec)

FLOAT(M,D)或REAL(M,D)或DOUBLE PRECISION(M,D)。這裏,「(M,D)」表示該值一共顯示M位整數。當中D位位於小數點後面。
好比,定義爲FLOAT(7,4)的一個列可以顯示爲-999.9999。

MySQL保存值時進行四捨五入,所以假設在FLOAT(7,4)列內插入999.00009,近似結果是999.0001。

相關文章
相關標籤/搜索