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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access