ROUND() Function in MySQL
Last Updated :
24 Apr, 2023
The ROUND() function in MySQL is used to round a number to a specified number of decimal places. If no specified number of decimal places is provided for round-off, it rounds off the number to the nearest integer.
Syntax
ROUND(X, D)
Parameter Explanation
This method accepts two parameters in the syntax, as mentioned above and described below -
- X: The number which to is rounded.
- D: Number of decimal places up to which the given number is to be rounded. It is optional. If not given it round off the number to the closest integer. If it is negative, then the number is rounded to the left side of the decimal point.
- Returns: It returns the number after rounding to the specified places.
Example-1
Rounding off a number when D is not specified.
Rounding a Negative number.
Query:
SELECT ROUND(-10.11) AS Rounded_Number;
Output:
Rounding a Positive number.
Query:
SELECT ROUND(100.61) AS Rounded_Number;
Output:
Example-2
Rounding off a number when D is negative(-ve).
Rounding a Negative number.
Query:
SELECT ROUND(-1567.1100, -3) AS Rounded_Number;
Output:
Rounding a Positive number.
Query:
SELECT ROUND(1016.6089, -1) AS Rounded_Number;
Output:
Example-3
Rounding off a number when D is positive(+ve).
Rounding a Negative number up to 2 decimal places.
Query:
SELECT ROUND(-1567.1160, 2) AS Rounded_Number;
Output:
Rounding a Positive number up to three decimal places.
Query:
SELECT ROUND(1016.6019, 3) AS Rounded_Number;
Output:
Example-4
The ROUND Function can also be used to find the rounded values for the column data. In this example, we are going to find rounded values for the Price column. To demonstrate create a table named Product.
Query:
CREATE TABLE Product(
Product_id INT AUTO_INCREMENT,
Product_name VARCHAR(100) NOT NULL,
Buying_price DECIMAL(13, 6) NOT NULL,
Selling_price DECIMAL(13, 6) NOT NULL,
Selling_Date Date NOT NULL,
PRIMARY KEY(Product_id)
);
Now insert some data into the Product table -
Query:
INSERT INTO
Product(Product_name, Buying_price, Selling_price, Selling_Date)
VALUES
('P6', 1060.865460, 1700.675400, '2020-08-26'),
('P2', 2000.154300, 3050.986700, '2020-08-27'),
('P1', 4000.874300, 5070.786500, '2020-08-28'),
('P2', 2090.654300, 3050.896500, '2020-09-01'),
('P3', 5900.543280, 7010.654700, '2020-09-04'),
('P4', 4000.353200, 4500.125400, '2020-09-05'),
('P5', 5010.768900, 6000.873200, '2020-09-08');
So, the Product Table is as -
Now, we are going to round off both Buying_price and Selling_price columns up to 2 decimal places.
Query:
SELECT Product_name, Buying_price,
ROUND(Buying_price, 2) Rounded_Bprice,
Selling_price, ROUND(Selling_price, 2)
Rounded_Sprice
FROM Product;
Output:
Similar Reads
RAND() Function in MySQL The RAND() function in MySQL is used to a return random floating-point value V in the range 0 <= V < 1.0. If we want to obtain a random integer R in the range i <= R < j, we have to use the expression : FLOOR(i + RAND() * (j â i)). Syntax : RAND(N) Parameter : This method accepts only on
3 min read
ORD() Function in MySQL ORD() function in MySQL is used to find the code of the leftmost character in a string . If the leftmost character is not a multibyte character, it returns ASCII value. And if the leftmost character of the string str is a multibyte character, ORD returns the code for that character, calculated from
3 min read
SECOND() Function in MySQL SECOND() function in MySQL is used to return the second portion of a specified time or date-time value. The first parameter in this function will be the date/Date Time. This function returns the seconds from the given date value. The return value (seconds) will be in the range of 0 to 59. In this fu
2 min read
TRUNCATE() Function in MySQL The TRUNCATE() function in MySQL is a valuable tool for manipulating numerical values by removing their decimal parts without rounding. It allows us to limit the precision of numbers to a specified number of decimal places or even truncate them to the nearest integer. In this article, We will learn
6 min read
TAN() Function in MySQL TAN() function : This function in MySQL is used to return the tangent of a specified number. In any right triangle, the tangent of an angle is the length of the opposite side divided by the length of the adjacent side. Similarly, this can also be defined as tangent of x is the sine of x divided by t
1 min read
SUM() Function in MySQL The SUM() function in MySQL is a powerful aggregate function used to calculate the total sum of values in a numeric column. By summing up the values in the specified column, this function helps in generating overall totals and performing calculations that provide meaningful insights from our data. I
4 min read
OCT() function in MySQL OCT() function in MySQL is used to convert decimal number to octal. It returns equivalent octal value of a decimal number. Syntax : OCT(number) Parameter : This method accepts only one parameter. number : The decimal number which we want to convert. Returns : It returns octal value of a decimal numb
2 min read
PI() function in MySQL PI() function in MySQL is used to return the Pi value. The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally. Syntax : PI() Parameter : This method does not accept any parameter. Returns : It returns the Pi value i.e. 3.141593. Example-1 :
2 min read
SIGN() Function in MySQL SIGN() function in MySQL is used to return the sign of the given number. It returns 1 if the number is positive, -1 if the number is negative and 0 for zero. Syntax : SIGN(X) Parameter : SIGN() function accepts one parameter as input and will give you the results in values like Positive(+1),Negative
1 min read
SQRT() Function in MySQL The SQRT() function in MySQL calculates the square root of a non-negative number, returning NULL for negative inputs. It is a built-in function that provides high precision and is optimized for performance and making it ideal for mathematical and scientific applications.In the article, we will cover
3 min read