MySQL Numeric Functions
Numeric functions round, truncate and transform numbers. They matter most for money and statistics, where getting the rounding right is part of getting the answer right.
Rounding
| Function | Example | Result |
|---|---|---|
ROUND(x) |
ROUND(2.5) |
3 |
ROUND(x, d) |
ROUND(3.14159, 2) |
3.14 |
ROUND(x, -d) |
ROUND(1234, -2) |
1200 |
TRUNCATE(x, d) |
TRUNCATE(3.789, 1) |
3.7 (cuts, never rounds) |
CEIL(x) / CEILING(x) |
CEIL(2.1) |
3 (next integer up) |
FLOOR(x) |
FLOOR(2.9) |
2 (next integer down) |
SELECT ROUND(7.456, 2) AS round2,
ROUND(7.456) AS round0,
ROUND(1250, -2) AS round_hundreds,
TRUNCATE(7.456, 2) AS truncated,
CEIL(7.01) AS ceil_up,
FLOOR(7.99) AS floor_down,
CEIL(-2.5) AS ceil_neg,
FLOOR(-2.5) AS floor_neg;+--------+--------+----------------+-----------+---------+------------+----------+-----------+
| round2 | round0 | round_hundreds | truncated | ceil_up | floor_down | ceil_neg | floor_neg |
+--------+--------+----------------+-----------+---------+------------+----------+-----------+
| 7.46 | 7 | 1300 | 7.45 | 8 | 7 | -2 | -3 |
+--------+--------+----------------+-----------+---------+------------+----------+-----------+
For negative numbers, CEIL moves towards zero and FLOOR away from it.
Exact versus approximate rounding
ROUND on an exact DECIMAL value rounds halves away from zero (2.5 → 3, -2.5 → -3). On a DOUBLE it uses the C library, which rounds halves to even (2.5 → 2):
SELECT ROUND(2.5) AS decimal_2_5,
ROUND(-2.5) AS decimal_neg,
ROUND(2.5e0) AS double_2_5,
ROUND(3.5e0) AS double_3_5;+-------------+-------------+------------+------------+
| decimal_2_5 | decimal_neg | double_2_5 | double_3_5 |
+-------------+-------------+------------+------------+
| 3 | -3 | 2 | 4 |
+-------------+-------------+------------+------------+
Store money in DECIMAL and this surprise never reaches your invoices.
Practical example: prices with VAT
SELECT name,
unit_price,
ROUND(unit_price * 1.21, 2) AS with_vat,
CEIL(unit_price) - 0.01 AS charm_price,
FLOOR(units_in_stock / 12) AS full_boxes,
units_in_stock % 12 AS loose_units
FROM products
WHERE product_id IN (1, 6, 13);+--------------------+------------+----------+-------------+------------+-------------+
| name | unit_price | with_vat | charm_price | full_boxes | loose_units |
+--------------------+------------+----------+-------------+------------+-------------+
| Espresso Beans 1kg | 24.50 | 29.65 | 24.99 | 3 | 4 |
| Butter Croissant | 1.60 | 1.94 | 1.99 | 10 | 0 |
| Gala Apples 1kg | 2.99 | 3.62 | 2.99 | 7 | 6 |
+--------------------+------------+----------+-------------+------------+-------------+
FLOOR(stock / 12) and stock % 12 split a stock count into boxes of twelve and the rest; stock DIV 12 would give the same number of full boxes.
Other math functions
| Function | Returns | Example | Result |
|---|---|---|---|
ABS(x) |
absolute value | ABS(-7) |
7 |
SIGN(x) |
-1, 0 or 1 | SIGN(-7) |
-1 |
MOD(a, b), a % b |
remainder | MOD(17, 5) |
2 |
POWER(x, y), POW |
x to the power y | POWER(2, 10) |
1024 |
SQRT(x) |
square root | SQRT(16) |
4 |
EXP(x), LN(x), LOG10(x), LOG2(x) |
exponential and logarithms | LOG10(1000) |
3 |
PI() |
π | PI() |
3.141593 |
GREATEST(a, b, ...) / LEAST(...) |
largest / smallest argument | GREATEST(3, 9, 4) |
9 |
RAND() |
random number in [0, 1) | different on every call |
SELECT ABS(-7) AS abs_value, SIGN(-7) AS sign_value, MOD(17, 5) AS remainder,
POWER(2, 10) AS pow, SQRT(16) AS root, ROUND(SQRT(2), 4) AS root2,
PI() AS pi, GREATEST(3, 9, 4) AS biggest, LEAST(3, 9, 4) AS smallest;+-----------+------------+-----------+------+------+--------+----------+---------+----------+
| abs_value | sign_value | remainder | pow | root | root2 | pi | biggest | smallest |
+-----------+------------+-----------+------+------+--------+----------+---------+----------+
| 7 | -1 | 2 | 1024 | 4 | 1.4142 | 3.141593 | 9 | 3 |
+-----------+------------+-----------+------+------+--------+----------+---------+----------+
GREATEST and LEAST compare their arguments within one row, unlike the aggregates MAX and MIN, which compare values across rows. They are handy for clamping: GREATEST(price - discount, 0) never goes below zero. Note that they return NULL if any argument is NULL.
Converting numbers
CAST(x AS type) converts between types; the numeric targets are SIGNED, UNSIGNED, DECIMAL(p,s), DOUBLE and FLOAT (MySQL 8.0.17+):
SELECT CAST(3.7 AS SIGNED) AS to_int,
CAST('42' AS UNSIGNED) + 1 AS from_text,
CAST(10 / 3 AS DECIMAL(6,2)) AS to_decimal,
10 / 3 AS plain_division,
CONV(255, 10, 16) AS hex_value,
CONV('ff', 16, 2) AS binary_value;+--------+-----------+------------+----------------+-----------+--------------+
| to_int | from_text | to_decimal | plain_division | hex_value | binary_value |
+--------+-----------+------------+----------------+-----------+--------------+
| 4 | 43 | 3.33 | 3.3333 | FF | 11111111 |
+--------+-----------+------------+----------------+-----------+--------------+
Casting to an integer rounds (3.7 becomes 4); use FLOOR or TRUNCATE first if you want to cut the decimals.
Test yourself
What is TRUNCATE(9.99, 1)?
- 10.0
- 9.9
- 9.99
- 10
What does FLOOR(-3.2) return?
- -3
- -4
- 3
- -3.2
What is the difference between GREATEST(a, b) and MAX(a)?
- None
- GREATEST compares arguments within a row; MAX compares values across rows
- MAX works on text only
- GREATEST is an aggregate