MySQL ROUND() Function
MySQL ROUND() Function: Rounding Numbers to Desired Precision
The ROUND() function rounds a number x to d decimal places. If you omit d, MySQL defaults to 0 and rounds to the nearest whole integer.
You can pass a positive d to round fractional cents, 0 to drop fractions, or a negative d to round tens, hundreds, or thousands left of the decimal point. MySQL's exact tie-breaking rule depends on whether the input is exact (DECIMAL/INT) or floating-point (DOUBLE/FLOAT).
Syntax
ROUND(x)
ROUND(x, d)
x: Numeric expression to round.d: Optional integer specifying decimal places (defaults to0; negative values round left of the decimal point).
Practical Example
Suppose demo_orders stores pre-tax subtotal values and a 0.0825 tax rate. You can round tax to 2 decimal places (ROUND(..., 2)) and also round order totals to the nearest $10 (ROUND(..., -1)) for executive summary buckets.
SELECT
order_id,
subtotal,
ROUND(subtotal * 0.0825, 2) AS sales_tax,
ROUND(subtotal, -1) AS nearest_ten_dollars
FROM demo_orders
WHERE order_id BETWEEN 201 AND 204;
| order_id | subtotal | sales_tax | nearest_ten_dollars |
|---|---|---|---|
| 201 | 124.99 | 10.31 | 120.00 |
| 202 | 125.00 | 10.31 | 130.00 |
| 203 | 89.40 | 7.38 | 90.00 |
| 204 | NULL | NULL | NULL |
On order 202, 125.00 sits halfway between 120 and 130. Because subtotal is an exact DECIMAL, ROUND(125.00, -1) rounds half away from zero to 130.00.
Common Mistakes & NULL Behavior
- DECIMAL vs DOUBLE tie-breaking: Exact types (
INT,DECIMAL) use round-half-away-from-zero (ROUND(2.5)=3,ROUND(-2.5)=-3). Approximate floating-point numbers (2.5e0,FLOAT,DOUBLE) depend on the C library and typically use round-half-to-even (ROUND(2.5e0)=2,ROUND(3.5e0)=4). Store currency inDECIMALto keep financial rounding deterministic. - Expecting ROUND() to add trailing zeros on floats:
ROUND(12.3, 2)on aDOUBLEprints12.3, not12.30. UseFORMAT()orCAST(... AS DECIMAL(10, 2))for display padding. - NULL propagation: If
xordisNULL,ROUND()returnsNULL.
Frequently Asked Questions
What happens when d is negative in MySQL ROUND()?
Negative d rounds digits to the left of the decimal point to zero. For instance, ROUND(1460, -2) returns 1500.
How does ROUND() differ from TRUNCATE()?
ROUND() adjusts the kept digit up or down based on the dropped digit (ROUND(4.89, 1) = 4.9), whereas TRUNCATE() simply cuts off remaining digits (TRUNCATE(4.89, 1) = 4.8).
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.