ROUND
ROUND in SQL is a function utilized to round numerical field to the nearest specified decimal or integer.
ROUND(X, D)
- x: This parameter signifies the number that needs to be rounded.
- d: This parameter specifies the number of decimals to keep after rounding.
Example
Output
Explanation
The ROUND function rounds a number to a specific decimal. The example shows rounding a number, 12345.6789, to one decimal point, resulting in 12345.7.
ROUND(value numeric, dp integer) RETURNS numeric
- value numeric: A numerical expression which is to be rounded to the defined number of decimal places.
- dp integer: Specifies the number of decimal places to which the given number should be rounded. When not provided, the function defaults to zero thus rounding to the nearest whole integer.
Example
Output
Explanation
The ROUND
function in PostgreSQL rounds a number to a certain number of decimal places as specified. In the provided example, the number 123.4567
is rounded to two decimal places, resulting in 123.46
.
ROUND( numeric_expression , length [ , function ] )
- numeric_expression: This is the number you want to round. It can be a numeric data type like integer, float or decimal etc.
- length: This is the precision to which you want to round numeric_expression. A positive value for length rounds numeric_expression to the corresponding decimal places to the right of the decimal point. A negative value for length rounds numeric_expression to the absolute value of the length to the left of the decimal point. Length is an optional parameter.
- function: This is an optional argument that specifies the operation to perform on numeric_expression after it’s been rounded to the specified length. The function parameter can take one of two values
Example
Output
Explanation
This example shows the usage of the ROUND function in SQL Server. The ROUND function rounds the number 123.4567 to 2 decimal places. The result, as expected, is 123.46.
ROUND(value, decimal_places)
- value: The numeric value that is to be rounded. This can be a number, or any expression that resolves to a number.
- decimal_places: A non-negative integer value that represents the place to which the number will be rounded. If it’s omitted or 0, the function will round the number to the nearest whole number. If it’s a positive number, the function will round the value to the specified number of decimal places. If it’s a negative number, the function will round the value to the left of the decimal point.
Example
Output
Explanation
The ROUND function in Oracle is utilized to round a number to a certain decimal place. It takes two arguments: the column or expression to be rounded, and the decimal position to which it should be rounded. The example code rounds the number 45.926 to two decimal places. The result is 45.93.
ROUND(X, Y)
- x: This is the number that needs to be rounded. It can be any numeric value including INTEGER, REAL, TEXT or BLOB that can be numerically interpreted. If X is NULL, ROUND(X,Y) will return NULL.
- y: It denotes the decimal places to which X will be rounded. If Y is not specified, then X will be rounded to the nearest whole number. When Y is a negative value, X will be rounded to the Yth place left of the decimal point. If Y is NULL, then ROUND(X,Y) will return NULL.
Example
Output
Explanation
The code example above shows the usage of the ROUND()
function in SQL. This function is used to round the given number to the specified number of decimal places. In the example, 123.456
is rounded to 2
decimal places, resulting in 123.46
.