w3resource

MySQL TRUNCATE() function

TRUNCATE() function

MySQL TRUNCATE() returns a number after truncated to certain decimal places. The number and the number of decimal places are specified as arguments of the TRUNCATE function.

Syntax:

TRUNCATE(N, D);

Arguments:

Name Description
N A number which is to be truncated up to D decimal places.
D A number indicating up to how many decimal places, N is to be truncated.

Note: When the value of ‘D’ is 0 the results have no fractional part and the ‘D’ digits left of the decimal point of the value ‘N’ become 0 when the value of ‘D’ is negative.

Syntax Diagram:

MySQL TRUNCATE() Function - Syntax Diagram

MySQL Version: 5.6


Example of MySQL TRUNCATE() function

Code:

SELECT TRUNCATE(2.465,1);

Explanation:

The above MySQL statement will return a value truncating 2.465 up to 1 decimal place.

Sample Output:

mysql> SELECT TRUNCATE(2.465,1);
+-------------------+
| TRUNCATE(2.465,1) |
+-------------------+
|               2.4 | 
+-------------------+
1 row in set (0.00 sec)

Example :TRUNCATE() function with negative decimal places

Code:

SELECT TRUNCATE(142.465,-2);

Explanation:

The above MySQL statement will return a value truncating 142.465 up to -2 decimal places.

Sample Output:

mysql> SELECT TRUNCATE(142.465,-2);
+----------------------+
| TRUNCATE(142.465,-2) |
+----------------------+
|                  100 | 
+----------------------+
1 row in set (0.00 sec)

All Mathematical Functions

MySQL Mathematical Functions, slide presentation

Previous: TAN()
Next: FORMAT()



Follow us on Facebook and Twitter for latest update.




We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook