Home  >  Article  >  Database  >  How to use the FLOOR function to round down a value in MySQL

How to use the FLOOR function to round down a value in MySQL

WBOY
WBOYOriginal
2023-07-12 10:34:361579browse

How to use the FLOOR function in MySQL to round down a value

In MySQL, the FLOOR function provides the function of rounding down, which can round a value down to an integer or a specified decimal. number of digits. This article will introduce how to use the FLOOR function in MySQL and give corresponding code examples.

First, let’s learn how to use the FLOOR function. The syntax of the FLOOR function is as follows:

FLOOR(x)

Among them, x represents the value that needs to be rounded down.

Next, we illustrate the use of the FLOOR function through several examples.

Example 1: Round down to an integer

Suppose we have a table students, which contains student performance information. Now, we need to process the results by rounding them down.

SELECT FLOOR(score) AS rounded_score
FROM students;

The above code will return a new column rounded_score, which contains the result of each student's score rounded down.

Example 2: Rounding down to two decimal places

If we need to round a value down to a specified number of decimal places, we can use the FLOOR function combined with the numerical function to achieve this .

Suppose we have a table sales, which stores sales information. Now we need to round the sales down to two decimal places.

SELECT ROUND(sales, 2) AS rounded_sales
FROM sales;

The above code will return a new column rounded_sales, which contains the result of each sales volume rounded down to two decimal places.

It should be noted that the result returned by the FLOOR function is a floating point number or a DOUBLE type value. If you need to convert it to an integer, you can use the CAST function or pass it as a parameter to the ROUND function.

To sum up, this article introduces the method of using the FLOOR function to round down a value in MySQL. By properly combining the FLOOR function with other numerical functions, we can perform flexible processing and operations on numerical values. I hope this article will help you use the FLOOR function in MySQL.

The above is the detailed content of How to use the FLOOR function to round down a value in MySQL. 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