开发者

MySQL Rounding functions

I am looking for a ROUND() type function that would allow me to round numbers to 1 decimal place but also to the nearest 0开发者_StackOverflow.5.

To illustrate:

19.425 => 19.5
19.124 => 19.0

Similarly:

12.654 => 12.5
12.845 => 13.0


As vissi said, to get the result you want you'll need 2 round statements. (To get to 1 decimal place)

SELECT ROUND(ROUND(19.425 * 2) / 2, 1) #19.5
SELECT ROUND(ROUND(19.124 * 2) / 2, 1) #19.0
SELECT ROUND(ROUND(12.654 * 2) / 2, 1) #12.5
SELECT ROUND(ROUND(12.845 * 2) / 2, 1) #13.0


You can multiply your number by two, round and then divide by two. Note, that the result may still be not very accurate (sth like 19.5000000000001).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜