# Python math.trunc() – Truncate Decimal Part

Contents

## Python math.trunc()

math.trunc(x) function truncates the decimal value and returns the real or integral value of x.

## Syntax

The syntax to call trunc() function is

``math.trunc(x)``

where

If x is infinity, then trunc(x) raises OverflowError.

If x is nan, then trunc(x) raises ValueError.

## Examples

### 1. Truncate float value

In the following program, we truncate the float value, x = 3.1415 using math.trunc().

Python Program

``````import math

x = 3.1415
result = math.trunc(x)
print('trunc(x) :', result)``````
Run Code Copy

Output

``trunc(x) : 3``

### 2. Truncate negative float value

In the following program, we truncate the negative float value, x = -3.1415, using math.trunc().

Python Program

``````import math

x = -3.1415
result = math.trunc(x)
print('trunc(x) :', result)``````
Run Code Copy

Output

``trunc(x) : -3``

### 3. Truncate infinity

In the following program, we truncate the value of infinity.

Python Program

``````import math

x = math.inf
result = math.trunc(x)
print('trunc(x) :', result)``````
Run Code Copy

Output

``OverflowError: cannot convert float infinity to integer``

### 4. Truncate integer value

In the following program, we truncate an integer value.

Python Program

``````import math

x = 3
result = math.trunc(x)
print('trunc(x) :', result)``````
Run Code Copy

Output

``trunc(x) : 3``

### 5. Truncate NaN

In the following program, we truncate the NaN value.

Python Program

``````import math

x = math.nan
result = math.trunc(x)
print('trunc(x) :', result)``````
Run Code Copy

Output

``ValueError: cannot convert float NaN to integer``

## Summary

In this Python Math tutorial, we learned the syntax of, and examples for math.trunc() function.

## Related Tutorials

Code copied to clipboard successfully 👍