我是 mysql 的新手,需要一些基本的东西。
我需要像这样四舍五入小数:
21,4758 应该是 21,48
0,2250 应为 0,22
23,0850 应为 23,08
22,9950 应该是 22,99
我尝试了很多事情但没能成功。
Thanks.
尝试这个:
// round the value to two decimal places
SELECT ROUND(<YOUR_FIELD>, 2) field FROM <YOUR_TABLE>
// use truncate if you don't wan't to round the actual value
SELECT TRUNCATE(<YOUR_FIELD>, 2) field FROM <YOUR_TABLE>
// you can also use round or truncate depending whether the third decimal is > 5
SELECT IF(SUBSTR(<YOUR_FIELD>, 5, 1) > 5,
ROUND(<YOUR_FIELD>, 2),
TRUNCATE(<YOUR_FIELD>, 2)) field
FROM <YOUR_TABLE>;
以上并不是完整的解决方案,但也许它会为您指明正确的方向。
阅读文档mysql round() http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_round and mysql truncate() http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_truncate
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)