TRUNCATE( )

This MySQL function returns a number equivalent to its first argument, removing any digits beyond the number of decimal places specified in the second argument.

Syntax

Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

TRUNCATE(number, number)

Explanation

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 is dropped, and the number of positions given is zeroed out in the remaining integer.

Examples

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 digis (starting from the right) of the integer are changed to zeros.