Name
TRUNCATE( )
Synopsis
TRUNCATE(number
,number
)
This function returns a number given in the first argument with the
digits beyond the number of decimal places specified in the second
argument, truncated. It does not round the number—use the
ROUND( )
function instead. If a 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 is dropped, and the number of
positions given is zeroed out for the integer.
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, 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, the decimal point and the fractional value are dropped, and because the second parameter is -2, the first two numbers (starting from the right) of the integer are changed to zeros.
Get MySQL in a Nutshell 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.