Name

TRUNCATE()

Synopsis

TRUNCATE(number, number)

This function returns a number equivalent to its first argument, removing any digits beyond the number of decimal places specified in the second argument. The function does not round the number; use the ROUND() function instead. If 0 is given for the second argument, the decimal point and the fractional value are dropped. If a negative number is given as the second argument, the decimal point and the fractional value are dropped, and the number of positions given is zeroed out in the remaining integer. Here is an example:

SELECT TRUNCATE(321.1234, 2) AS '+2',
TRUNCATE(321.1234, 0) AS '0',
TRUNCATE(321.1234, -2) AS '-2';

+--------+-----+-----+
| +2     | 0   | -2  |
+--------+-----+-----+
| 321.12 | 321 | 300 |
+--------+-----+-----+

Notice that for the first field in the results, the last two decimal places are dropped. For the second field, the decimal point and all of the fractional value are dropped. For the third field, the decimal point and the fractional value are dropped, and because the second parameter is –2, the two least significant digits (starting from the right) of the integer are changed to zeros.

Get MySQL in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.