MySQL provides several numeric functions that allow you to perform mathematical calculations on numeric values. These functions help with operations like rounding, modulus, absolute values, and more.
The ABS() function returns the absolute (positive) value of a number.
SELECT ABS(number);
SELECT ABS(-42) AS AbsoluteValue;
AbsoluteValue |
---|
42 |
The CEIL() or CEILING() function rounds a number up to the nearest integer.
SELECT CEIL(number);
SELECT CEILING(number);
SELECT CEIL(4.2) AS CeilValue;
CeilValue |
---|
5 |
The FLOOR() function rounds a number down to the nearest integer.
SELECT FLOOR(number);
SELECT FLOOR(4.8) AS FloorValue;
FloorValue |
---|
4 |
The ROUND() function rounds a number to a specified number of decimal places.
SELECT ROUND(number, decimal_places);
SELECT ROUND(3.14159, 2) AS RoundedValue;
RoundedValue |
---|
3.14 |
The MOD() function returns the remainder of the division of one number by another (modulus).
SELECT MOD(dividend, divisor);
SELECT MOD(10, 3) AS Remainder;
Remainder |
---|
1 |
Function | Description |
---|---|
ABS() | Returns the absolute value of a number. |
CEIL() | Rounds a number up to the nearest integer. |
FLOOR() | Rounds a number down to the nearest integer. |
ROUND() | Rounds a number to a specified number of decimal places. |
MOD() | Returns the remainder of a division. |
POWER() | Returns a number raised to the power of another number. |
SQRT() | Returns the square root of a number. |
SIGN() | Returns the sign of a number (-1, 0, 1). |
RAND() | Generates a random floating-point number. |
TRUNCATE() | Truncates a number to a specified decimal place. |
PI() | Returns the value of π (3.141593). |
EXP() | Returns e raised to the power of a given number. |
LOG() | Returns the natural logarithm (base e) of a number. |
LOG10() | Returns the base 10 logarithm of a number. |
GREATEST() | Returns the largest value in a list of values. |
LEAST() | Returns the smallest value in a list of values. |