Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Round Function

Tags:

sql

rounding

round(45.923,-1) gives a result of 50. Why is this? How it is calculated?

(sorry guys i was mistaken with earlier version of this question suggesting value was 46)

like image 647
hrishi Avatar asked Jul 10 '09 10:07

hrishi


People also ask

What is the ROUND function in SQL?

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

How do you ROUND up in SQL?

In Oracle, MySQL, and PostgreSQL, you can use either the CEIL() or CEILING() function to round up.


2 Answers

The SQL ROUND() function rounds a number to a precision...

For example:

round(45.65, 1) gives result = 45.7

round(45.65, -1) gives result = 50

because the precision in this case is calculated from the decimal point. If positive then it'll consider the right side number and round it upwards if it's >= 5, and if <=4 then round is downwards... and similarly if it's negative then the precision is calculated for the left hand side of decimal point... if it's >= 5

for example round(44.65, -1) gives 40 but round(45.65, -1) gives 50...

like image 155
S M Kamran Avatar answered Oct 06 '22 01:10

S M Kamran


ROUND(748.58, -1) 750.00

the second parameter: Lenght, is the precision to which numeric_expression is to be rounded. length must be an expression of type tinyint, smallint, or int. When length is a positive number, numeric_expression is rounded to the number of decimal positions specified by length. When length is a negative number, numeric_expression is rounded on the left side of the decimal point, as specified by length.

From

like image 21
Elzo Valugi Avatar answered Oct 06 '22 01:10

Elzo Valugi