今天遇到一個問題,格式化浮點數的問題,用format(col,2)保留兩位小數點,出現一個問題,例如
SELECT FORMAT(12562.6655,2);
結果:12,562.67
查看文檔:Formats the number X to a format like '#,###,###.##', rounded to D decimal places, and returns the result as a string. If D is
0, the result has no decimal point or fractional part.整數部分超過三位的時候以逗號分割,並且返回的結果是string類型的。
mysql> SELECT FORMAT(12332.123456, 4);
-> '12,332.1235'
mysql> SELECT FORMAT(12332.1,4);
-> '12,332.1000'
mysql> SELECT FORMAT(12332.2,0);
-> '12,332'
沒有達到預期結果,想要的結果不要以逗號分隔,
select truncate(4545.1366,2);
結果:4545.13,直接截取不四舍五入,還是有問題。
select convert(4545.1366,decimal(10,2));
結果:4545.14,達到預期。
或 CAST
SELECT CAST(
'4545.1366'
AS DECIMAL(10,2));
convert、cast的用法
字符集轉換 : CONVERT(xxx USING utf-8) 類型轉換和SQL Server一樣,就是類型參數有點點不同 : CAST(xxx AS 類型) , CONVERT(xxx,類型),類型必須用下列的類型: 可用的類型 二進制,同帶binary前綴的效果 : BINARY 字符型,可帶參數 : CHAR() 日期 : DATE 時間: TIME 日期時間型 : DATETIME 浮點數 : DECIMAL 整數 : SIGNED 無符號整數 : UNSIGNED
mysql中float類型不定義精度時,對float列的檢索可能不正確。
1.創建表t3,col1_float定義為float類型不指定精度
mysql> CREATE TABLE `t3` (
`col1_float` float DEFAULT NULL,
`col2_double` double DEFAULT NULL,
`col3_decimal` decimal DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Query OK, 0 rows affected
2.插入數據
mysql中float類型不指定精度時,會按照實際的精度顯示
mysql> insert into t3 values(100.123,200.123,300.123);
Query OK, 1 row affected
mysql> select * from t3;
+------------+-------------+--------------+
| col1_float | col2_double | col3_decimal |
+------------+-------------+--------------+
| 100.123 | 200.123 | 300 |
+------------+-------------+--------------+
1 row in set
3.對float類型不指定精度時,默認等值查詢結果為空.
如t3表中存在col1_float為100.123的記錄,但由於定義col1_float的類型時沒有指定float的精度,float精度會在最后的一位或多位丟失,比如15.9實際值為15.899993等
在對float列進行等值查詢的時候檢索不到記錄。
mysql> select * from t3 where col1_float=100.123;
Empty set
mysql> show warnings;
Empty set
4.對double類型,則沒有這種情況
mysql> select * from t3 where col2_double=200.123;
+------------+-------------+--------------+
| col1_float | col2_double | col3_decimal |
+------------+-------------+--------------+
| 100.123 | 200.123 | 300 |
+------------+-------------+--------------+
1 row in set
4.對decimal類型,如果不指定精度, 會對小數位進行截斷
mysql> select * from t3 where col3_decimal=300;
+------------+-------------+--------------+
| col1_float | col2_double | col3_decimal |
+------------+-------------+--------------+
| 100.123 | 200.123 | 300 |
+------------+-------------+--------------+
1 row in set
部分內容摘自:http://blog.csdn.net/lwei_998/article/details/40979351
Mysql中針對不定義精度的float類型進行等值查詢的解決方法:
a).使用like
mysql> select * from t3 where col1_float like 100.123;
+------------+-------------+--------------+
| col1_float | col2_double | col3_decimal |
+------------+-------------+--------------+
| 100.123 | 200.123 | 300 |
+------------+-------------+--------------+
1 row in set
b).使用format進行轉換
mysql> select * from t3 where format(col1_float ,3) = format(100.123 ,3);
+------------+-------------+--------------+
| col1_float | col2_double | col3_decimal |
+------------+-------------+--------------+
| 100.123 | 200.123 | 300 |
+------------+-------------+--------------+
1 row in set
c). 修改數據類型,指定精度和標度。
mysql> alter table t3 modify col1_float float(6,3);
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from t3 where col1_float=100.123;
+------------+-------------+--------------+
| col1_float | col2_double | col3_decimal |
+------------+-------------+--------------+
| 100.123 | 200.123 | 300 |
+------------+-------------+--------------+
1 row in set
如果小數點后面位數固定可以用decimal類型.也可以解決問題.精確數據一定要使用decimal,如資金數據。