MySQL TRUNCATE() Function
The TRUNCATE()
function in MySQL removes decimal places from a number without any rounding. It simply chops off the digits beyond the specified number of decimal places.
TRUNCATE(): Definition and Usage
This function is useful when you need to reduce the precision of a number without altering its value by rounding up or down. It's different from rounding functions like ROUND()
, which adjust the number to the nearest value.
Related Functions
For related functions that modify numbers, check out FLOOR()
(rounds down), CEIL()
/CEILING()
(rounds up), and ROUND()
(rounds to a specified number of decimal places).
Syntax
Syntax
TRUNCATE(number, decimals)
Parameter Values
Parameter | Description |
---|---|
number |
The number you want to truncate. This is required. |
decimals |
The number of decimal places to keep. Digits beyond this point are removed. This is required. |
Examples
Truncating to Two Decimal Places
This example truncates 135.375 to two decimal places, resulting in 135.37.
Syntax
SELECT TRUNCATE(135.375, 2);
Output
135.37
Truncating to Zero Decimal Places
Truncating to zero decimal places effectively removes all decimal places.
Syntax
SELECT TRUNCATE(345.156, 0);
Output
345