# What would be effect of negative value of second argument, which specifies the number of decimal places, on the output of MySQL TRUNCATE() function?

MySQLMySQLi Database

If we specify the negative value of the second argument then the digits before the decimal point would be deleted without rounded off. The number of digits to be deleted depends upon the value of the negative second argument. Following examples will demonstrate the change, depending upon the negative value of the second argument, in the output of TRUNCATE() function.

mysql> Select TRUNCATE(1789.456,-1);
+-----------------------+
| TRUNCATE(1789.456,-1) |
+-----------------------+
|                  1780 |
+-----------------------+
1 row in set (0.00 sec)  

The query above returns 1780 because the first digit before the decimal point is deleted due to -1 value of the second argument.

mysql> Select TRUNCATE(1789.456,-2);
+-----------------------+
| TRUNCATE(1789.456,-2) |
+-----------------------+
|                  1700 |
+-----------------------+
1 row in set (0.00 sec)  

The query above returns 1700 because two digits before decimal point are deleted due to -2 value of the second argument.

mysql> Select TRUNCATE(1789.456,-3);
+-----------------------+
| TRUNCATE(1789.456,-3) |
+-----------------------+
|                  1000 |
+-----------------------+
1 row in set (0.00 sec)  

The query above returns 1000 because three digits before decimal point are deleted due to -3 value of the second argument.

mysql> Select TRUNCATE(1789.456,-4);
+-----------------------+
| TRUNCATE(1789.456,-4) |
+-----------------------+
|                     0 |
+-----------------------+
1 row in set (0.00 sec)  

The query above returns 0 because a total number of digits before the decimal point is 4 and the value of the second argument is -4.

Published on 20-Feb-2018 17:54:27