MySQL TRUNCATE() Function
MySQL TRUNCATE() Function: Dropping Digits Without Rounding
The TRUNCATE() function cuts off a number x to d decimal places without rounding up or down. Unlike ROUND(), which examines dropped digits to adjust the result, TRUNCATE() simply chops extra digits toward zero.
Billing engines and tax rules sometimes require truncating fractional cents instead of rounding, or stripping trailing digits off large integers using a negative d parameter. Note that TRUNCATE() the math function is completely different from the TRUNCATE TABLE statement.
Syntax
TRUNCATE(x, d)
x: Numeric expression to truncate.d: Mandatory integer specifying decimal places to keep (0drops all decimals; negativedzeroes out digits left of the decimal point).
Practical Example
Suppose demo_orders calculates loyalty points as total_amount * 1.5, and your program awards only whole or tenth-point increments by truncating rather than rounding up.
SELECT
order_id,
total_amount,
total_amount * 1.5 AS raw_points,
TRUNCATE(total_amount * 1.5, 1) AS tenth_points,
TRUNCATE(total_amount * 1.5, 0) AS whole_points,
TRUNCATE(total_amount, -1) AS ten_dollar_tier
FROM demo_orders
WHERE order_id BETWEEN 1001 AND 1004;
| order_id | total_amount | raw_points | tenth_points | whole_points | ten_dollar_tier |
|---|---|---|---|---|---|
| 1001 | 59.99 | 89.985 | 89.9 | 89 | 50.00 |
| 1002 | 124.50 | 186.750 | 186.7 | 186 | 120.00 |
| 1003 | -19.99 | -29.985 | -29.9 | -29 | -10.00 |
| 1004 | NULL | NULL | NULL | NULL | NULL |
Look at order 1001: 89.985 truncates to 89.9 and 89, never rounding up to 90. Look at order 1003: -29.985 truncates toward zero to -29.
Common Mistakes & NULL Behavior
- Omitting the second argument: Unlike
ROUND(x), which defaultsdto0,TRUNCATE(x)throws a syntax error in MySQL. Always pass both arguments, e.g.TRUNCATE(x, 0). - Confusing TRUNCATE(x, 0) with FLOOR(x) on negatives: For
-19.99,TRUNCATE(-19.99, 0)returns-19(toward zero), whereasFLOOR(-19.99)returns-20(toward negative infinity). - NULL propagation: If either
xordisNULL,TRUNCATE()returnsNULL.
Frequently Asked Questions
What happens if d is negative in TRUNCATE(x, d)?
MySQL zeroes out abs(d) digits to the left of the decimal point without rounding. For example, TRUNCATE(498, -2) returns 400.
Does TRUNCATE(x, d) work on both DECIMAL and DOUBLE columns?
Yes. For DECIMAL inputs it returns an exact DECIMAL, and for DOUBLE inputs it returns a DOUBLE.
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.