how to get decimals only if number is not round in mysql
I'm trying to get a number formated as having 2 decimals if the number is not an integer and not having any if the number is an integer. Currently I'm using the following query
SELECT round(SUM(number),2) as Numb FROM table
And I get numbers开发者_如何学编程 like 1.00 which I'd like it to be 1
You'll have to use a case with modulus. Something like:
select
case (number mod 1 > 0)
when true then round(number, 2)
else round(number, 0)
end
from Numb
Results:
select
case (number mod 1 > 0)
when true then round(number, 2)
else round(number,0)
end as num
from Numb;
+------+
| num |
+------+
| 2 |
| 1.01 |
| 5 |
| 3.01 |
| 4.25 |
+------+
5 rows in set (0.00 sec)
精彩评论