Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - How to round up or down decimals?

I would like to be abble to round up or down 10.823. Expected result:

rounding down = 10.82
rounding up = 10.83

Knowing that round(10.823, 2) only rounds down. How to round it up?

like image 747
Marc Avatar asked May 17 '15 18:05

Marc


People also ask

How do you round up decimals in SQL Server?

SQL Server ROUND() Function The ROUND() function rounds a number to a specified number of decimal places.

Does round in SQL round up or down?

SQL ROUND Function You might have known CEILING and FLOOR, but ROUND is by far the most common. Rounding just means to round up from 5 or down from anything less. ROUND is unique because you can tell SQL which position you would like rounded.

How do you round a number down in SQL?

The SQL FLOOR() function rounded up any positive or negative decimal value down to the next least integer value. SQL DISTINCT along with the SQL FLOOR() function is used to retrieve only unique value after rounded down to the next least integer value depending on the column specified.


1 Answers

You are correct, round is the wrong tool for this job. Instead, you should use floor and ceiling. Unfortunately, they do not have a precision parameter like round, so you'd have to simulate it using division and multiplication:

SELECT FLOOR(value * 100) / 100 AS rounded_down,
       CEILING(value * 100) / 100 AS rounded_up
FROM   mytable
like image 177
Mureinik Avatar answered Oct 18 '22 02:10

Mureinik