×

MySQL ROUND() function

In this context, we will learn how we can use the MySQL ROUND() function with proper syntax and good examples.

Introduction of MySQL ROUND() function

The ROUND() function in MySQL is used to round a number to a specified number of decimal places. It will round off the number to the nearest integer when there no specified number of decimal places is provided for round-off.

Syntax of the MySQL ROUND() function

The syntax of the MySQL ROUND() function is given as follows:

ROUND(Y, Z);

Parameters or arguments used in MySQL ROUND() function:

There are two parameters accepted by the ROUND() function in MySQL, which are given as follows:

Y: The number which is to be rounded.

Z: Number of decimal places up to which the given number is to be rounded. It is optional. If not given, it rounds 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 will return the number after rounding to the specified places.

Application used for ROUND() function:

The ROUND() function can be used in the given below MySQL versions.:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Examples of MySQL ROUND() function:

Now we will look into some MySQL ROUND() function examples and will explore how we can use the ROUND function in MySQL.

Example 1:

Now we will Round off a number when D is not specified with the help of the ROUND function in MySQL.

a) In this example, we will Round off a Negative number.

SELECT ROUND(-8.11) AS Rounded_Number;

Output:

Rounded_Number
-8
1 row in set (0.00 sec)

b) In this example, we will Round off a Positive number.

SELECT ROUND(54.61) AS Rounded_Number;

Output:

Rounded_Number
54
1 row in set (0.00 sec)

Example 2:

Here, we will Round off a number when D is negative(-ve) with the help of the ROUND function in MySQL.

a) Rounding a Negative number.

SELECT ROUND(-1567.1100, -3) AS Rounded_Number;

Output:

Rounded_Number
-2000
1 row in set (0.00 sec)

b) Rounding a Positive number.

SELECT ROUND(1016.6089, -1) AS Rounded_Number;

Output:

Rounded_Number
1020
1 row in set (0.00 sec)

Example 3:

Here, we will Round off a number when D is positive(+ve) with the help of the ROUND function in MySQL.

a) Rounding a Negative number up to 2 decimal places.

SELECT ROUND(-1567.1160, 2) AS Rounded_Number;

Output:

Rounded_Number
-1567.12
1 row in set (0.00 sec)

b) Rounding a Positive number up to three decimal places.

SELECT ROUND(1016.6019, 3) AS Rounded_Number;

Output:

Rounded_Number
1016.602
1 row in set (0.00 sec)

Example 4:

When we require to find the rounded values for the column data, then we can take the help of the ROUND function. In this example, we are going to find rounded values for the Price column. For demonstration, we have created a table named ComROUNDity.

CREATE TABLE ComROUNDity(
ComROUNDity_id INT AUTO_INCREMENT,
ComROUNDity_name VARCHAR(100) NOT NULL,
Purchasing_price DECIMAL(13, 6) NOT NULL,
Selling_price DECIMAL(13, 6) NOT NULL,
Selling_Date Date NOT NULL,
PRIMARY KEY(ComROUNDity_id)
);

Now insert some data to the ComROUNDity table.

INSERT INTO
ComROUNDity(ComROUNDity_name, Purchasing_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 ComROUNDity Table is –

ComROUNDity_idComROUNDity_namePurchasing_priceSelling_priceSelling_Date
1P61060.8654601700.6754002020-08-26
2P22000.1543003050.9867002020-08-27
3P14000.8743005070.7865002020-08-28
4P22090.6543003050.8965002020-09-01
5P34000.3532007010.6547002020-09-04
6P44000.3532004500.1254002020-09-05
7P55010.7689006000.8732002020-09-08

Now, we are going to round off both Purchasing_price and Selling_price columns up to 2 decimal places.

SELECT ComROUNDity_name, Purchasing_price, ROUND(Purchasing_price, 2) Rounded_Bprice,
Selling_price, ROUND(Selling_price, 2) Rounded_Sprice
FROM ComROUNDity;

Output:

ComROUNDity_namePurchasing_priceRounded_BpriceSelling_priceRounded_Sprice
P61060.8654601060.871700.6754001700.68
P22000.1543002000.153050.9867003050.99
P14000.8743004000.875070.7865005070.79
P22090.6543002090.653050.8965003050.90
P35900.5432805900.547010.6547007010.65
P44000.3532004000.354500.1254004500.13
P55010.7689005010.77  

Summary:

In the above context, we have learned how we can use the ROUND() function in MySQL used to round a number to a specified number of decimal places.


Related Topics

MySQL Environmental Setup

The MySQL Environmental Setup MySQL is free, open-source, and cross-platform software, which can be downloaded from its official website. MySQL management system installs on Linux, macOS, and windows. MySQL requires a framework...

6 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

MySQL LIKE() function

In this context, we will learn how we can use the MySQL LIKE() function with proper syntax and good examples. Introduction of MySQL LIKE() function The LIKE function is a logical function...

4 minutes read.

find_in_set() function in MySQL

This Function is used for finding the position of a particular string from the list of strings. For suppose if the specified string is repeated multiple times, then this functions...

4 minutes read.

MySQL vs SQL

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL LOG() function

In this context, we will learn how we can use the MySQL LOG () function with proper syntax and good examples. Introduction of MySQL LOG() function The LOG() function in MySQL is...

3 minutes read.

MySQL Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL INSERT() Function

In this context, we will learn how we can use the MySQL INSERT() function with proper syntax and good examples. Introduction of MySQL INSERT() function Basically, in MySQL, the INSERT function is...

2 minutes read.

MySQL MONTHNAME() function

In this context, we will learn how we can use the MySQL MONTHNAME() Function with proper syntax and good examples. Introduction of MySQL MONTHNAME() function In MySQL the MONTHNAME() Function is used...

3 minutes read.

MySQL MAX function

The MAX function is a type of aggregation function that determines the maximum value of the table data. This function works on numerical data type values. If the table displays...

4 minutes read.

MySQL COALESCE function

This statement displays the first non-null value of the table data. The COALESCE function returns a null value when all values of the table are null or do not find...

3 minutes read.

MySQL SEC_TO_TIME() function

In this context, we will learn how we can use the MySQL SEC_TO_TIME() function with proper syntax and good examples. Introduction of MySQL SEC_TO_TIME() function This function in MySQL is used to...

2 minutes read.

MySQL variable

The MySQL variable is essential for storing data in the table. The variable declares data with a specific name or label to avoid confusion. This data label is used in...

6 minutes read.

MySQL GREATEST function

This statement displays the greatest values of the table. The GREATEST function returns a null value when the table contains a null value. The GREATEST function needs a minimum of...

2 minutes read.

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 minutes read.

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL CEIL() function

In this context, we will learn how we can use the MySQL CEIL () function with proper syntax and good examples. Introduction of MySQL CEIL() function Input is taken by the CEIL()...

3 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 minutes read.

MySQL ELT() function

In this context, we will learn how we can use the MySQL ELT() function with proper syntax and good examples. Introduction of MySQL ELT() function In the ELT function, the number field...

2 minutes read.