MySQL TRUNCATE() Function

The TRUNCATE() function in MySQL is used to truncate a number to a specified number of decimal places without rounding the value.


Definition and Usage

  • The TRUNCATE() function removes the decimal part of a number (without rounding), leaving only the specified number of decimal places.

Syntax

TRUNCATE(number, decimals)

Parameter Values

Parameter Description
number The numeric value that you want to truncate.
decimals The number of decimal places to truncate the number to.

Technical Details

  • Works in: From MySQL 4.0
  • Return Value: The truncated number, with the specified number of decimal places.

Examples

Example 1: Truncate to 2 decimal places

SELECT TRUNCATE(135.375, 2);

Output:

TRUNCATE(135.375, 2)
--------------------
135.37

In this example, the number 135.375 is truncated to 135.37 (2 decimal places).

Example 2: Truncate to 0 decimal places (round to integer)

SELECT TRUNCATE(345.156, 0);

Output:

TRUNCATE(345.156, 0)
--------------------
345

Here, the number 345.156 is truncated to 345 with 0 decimal places.


Use Cases

  • Financial Calculations: When you want to truncate a number (such as a price or measurement) without rounding it, for example, truncating values for fixed-point precision in financial systems.
  • Data Processing: When dealing with large numbers or floating-point calculations where precision is needed up to a certain number of decimals.

Related Functions

  • FLOOR(): Rounds a number down to the nearest integer.
  • CEIL() / CEILING(): Rounds a number up to the nearest integer.
  • ROUND(): Rounds a number to the nearest integer or specified decimal places.

Was this article helpful?