Home  >  Article  >  Database  >  How to use the ROUND function in MySQL to round a number

How to use the ROUND function in MySQL to round a number

王林
王林Original
2023-07-15 16:55:392794browse

How to use the ROUND function in MySQL to round a numerical value

In MySQL, the ROUND function is a commonly used function for rounding a numerical value. It rounds a float or double value to a specified number of decimal places. This article explains how to use the ROUND function and provides some code examples.

The syntax of the ROUND function is as follows:

ROUND (the value to be rounded, the number of decimal places to be retained)

Let’s look at some specific examples below.

Example 1: Rounding a floating point number to an integer

Suppose we have a floating point value of 3.7 and we want to round it to the nearest integer. We can use the following code to achieve this:

SELECT ROUND(3.7) AS RoundedNumber;

Running the above code, you will get the following results:

RoundedNumber
4

As you can see, 3.7 is rounded to 4, becoming the nearest integer.

Example 2: Retain a floating point number to a specific number of decimal places

Sometimes, we need to retain a floating point value to a specified number of decimal places. Let's say we have a numerical value of 3.78546 and we want to round it to two decimal places. We can use the following code to achieve this:

SELECT ROUND(3.78546, 2) AS RoundedNumber;

Run the above code and you will get the following result:

RoundedNumber
3.79

As you can see, 3.78546 is rounded to two decimal places and retained as 3.79.

It should be noted that when the number of digits to be retained is a negative number, the ROUND function will round to the number of digits in the integer. For example, if we change the code in Example 2 to hold to an integer number of digits, i.e. ROUND(3.78546, -1), then the result will be 4 because it is rounded to an integer number of digits.

Additionally, if the number of decimal places to be preserved is greater than the number of decimal places of the original value, the result will be the original value itself. For example, if we change the code in Example 2 to ROUND(3.78546, 4), the result will be 3.78546 because the original value only has 5 decimal places.

To sum up, using the ROUND function can conveniently perform numerical rounding operations. Whether it is rounding a floating point number to an integer or retaining a specified number of decimal places, we can easily do it with the help of the ROUND function.

I hope the introduction and code examples in this article can help you better use the ROUND function in MySQL to round numerical values.

The above is the detailed content of How to use the ROUND function in MySQL to round a number. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn