SoFunction
Updated on 2025-03-02

Usage of TRUNCATE function in MySQL

MySQL's TRUNCATE() function

TRUNCATE(X,D) is a system function that comes with MySQL.

Where X is the value and D is the number of digits that retain the decimal.

Its function is to intercept numerical values ​​according to the number of decimal places (the interception here is to intercept directly according to the number of reserved digits without rounding).

The rules are as follows

  • 1) When D is greater than 0, it is to operate on the decimal places of the numerical value X;
  • 2) When D is equal to 0, it is to remove the decimal part of the numerical value X and only the integer part is retained;
  • 3) When D is less than 0, it is to remove the decimal part of the value X, and replace the integer part with 0.

Function example

When X is a positive number

SELECT TRUNCATE(123.4567, 3); # 123.456
SELECT TRUNCATE(123.4567, 2); # 123.45
SELECT TRUNCATE(123.4567, 1); # 123.4
SELECT TRUNCATE(123.4567, 0); # 123
SELECT TRUNCATE(123.4567, -1); # 120
SELECT TRUNCATE(123.4567, -2); # 100
SELECT TRUNCATE(123.4567, -3); # 0

When X is negative

SELECT TRUNCATE(-123.4567, 3); # -123.456
SELECT TRUNCATE(-123.4567, 2); # -123.45
SELECT TRUNCATE(-123.4567, 1); # -123.4
SELECT TRUNCATE(-123.4567, 0); # -123
SELECT TRUNCATE(-123.4567, -1); # -120
SELECT TRUNCATE(-123.4567, -2); # -100
SELECT TRUNCATE(-123.4567, -3); # 0

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.