日期:2014-05-16  浏览次数:22046 次

求大虾指点,关于小数点后的有效数字
我数据库字段类型是double,保留位数是3,然后里面数值为0.000,0.040,0.045,如果我要通过sql语句来读取,去除末尾的0,让数值0.000时为0,0.040的时候为0.04

------解决方案--------------------
try:
SELECT IF(f1=0.000,0,CAST(f1 AS DECIMAL(12,2))) FROM tt
------解决方案--------------------
SQL code
mysql> select * from gn_Grade;
+-------+
| x     |
+-------+
| 0.000 |
| 0.040 |
| 0.045 |
+-------+
3 rows in set (0.00 sec)

mysql> select TRIM(TRAILING '0' FROM x)+0 from gn_Grade;
+-----------------------------+
| TRIM(TRAILING '0' FROM x)+0 |
+-----------------------------+
|                           0 |
|                        0.04 |
|                       0.045 |
+-----------------------------+
3 rows in set (0.00 sec)

mysql>