MySQL Functions To Round Numbers
MySQL provides several functions to round numbers. Here are the primary rounding functions:
ROUND(): This is the most commonly used rounding function. It rounds the number to the nearest whole number or the specified number of decimal places.
- Syntax:
ROUND(number, [decimal_places])
- Example:
ROUND(123.4567, 2)
would return123.46
.
CEIL() or CEILING(): These functions return the smallest integer value greater than or equal to the specified number. It essentially rounds up.
- Syntax:
CEIL(number)
orCEILING(number)
- Example:
CEIL(123.4567)
would return124
.
FLOOR(): This function returns the immense integer value that is less than or equal to the specified number. It rounds down.
- Syntax:
FLOOR(number)
- Example:
FLOOR(123.4567)
would return123
.
TRUNCATE(): This function truncates a number to the specified number of decimal places. It doesn’t round the number; instead, it removes excess decimals.
- Syntax:
TRUNCATE(number, decimal_places)
- Example:
TRUNCATE(123.4567, 2)
would return123.45
.
When using these functions, choosing the one that best fits the desired rounding behavior for your specific use case is essential.