I want to display only 1 decimal number and round it to the closest first decimal, but if the second decimal is 5 or above, round up and not down.
For example:
16.55 > 16.6
5.42 > 5.4
23.88 > 23.9
I tried the following:
SELECT ROUND(time, 1, 1) as time
And it mostly works, except for when the second decimal number is 5 and then it rounds it down, for example 16.55
would round to 16.5
4
ROUND
, according to the documentation has these parameters:
ROUND ( numeric_expression , length [ ,function ] )
so you have a numeric expression and you want to make it of a certain length by rounding it. The function is described as
Is the type of operation to perform. function must be tinyint, smallint, or int. When function is omitted or has a value of 0 (default), numeric_expression is rounded. When a value other than 0 is specified, numeric_expression is truncated.
So, if you pass something as a third parameter, then ROUND
will effectively truncate rather than round. Your call of
SELECT ROUND(time, 1, 1) as time
passes a third parameter, so you end up with a truncation. Since you wanted rounding instead, avoid passing a third parameter and call the function like this:
SELECT ROUND(time, 1) as time