klonq
klonq

Reputation: 3587

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.5.

To illustrate:

19.425 => 19.5
19.124 => 19.0

Similarly:

12.654 => 12.5
12.845 => 13.0

Upvotes: 1

Views: 2180

Answers (2)

anothershrubery
anothershrubery

Reputation: 21003

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

Upvotes: 9

vissi
vissi

Reputation: 2334

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).

Upvotes: 3

Related Questions