← Winnitude! Search  ·  Reader Mode (472 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
WWW.TUTORIALSPOINT.COM · 250 LINKS

Python math.trunc() Method

Selected Reading

Python math.trunc() Method


Previous Quiz Next

The Python math.trunc() method is used to truncate a floating-point number to its nearest integer towards zero. Truncation refers to removing the fractional part of the number without rounding it.

For example, math.trunc(3.14) will return 3 and math.trunc(-3.14) will return -3 as a result.

Syntax

Following is the basic syntax of the Python math .trunc() method −

math.trunc(x)

Parameters

This method accepts a numeric value (float or integer) as a parameter for which you want to truncate to its nearest integer towards zero.

Return Value

The method returns an integer, which represents the truncated value of the given value "x".

Example 1

In the following example, we are truncating a floating-point number "10.9" using the math.trunc() method −

import math
result = math.trunc(10.9)
print("The result obtained is:",result)         

Output

The output obtained is as follows −

The result obtained is: 10

Example 2

Here, we are truncating a negative floating-point number "-10.9" using the math.trunc() method −

import math
result = math.trunc(-10.9)
print("The result obtained is:",result)  

Output

Following is the output of the above code −

The result obtained is: -10

Example 3

Now, we truncate the floating-point number "15.0". Since there is no fractional part, the result remains the same −

import math
result = math.trunc(15.0)
print("The result obtained is:",result)  

Output

We get the output as shown below −

The result obtained is: 15

Example 4

In this example, we use a variable "x" to store the floating-point number "3.14159". We then truncate "x" to the nearest integer value −

import math
x = 3.14159
result = math.trunc(x)
print("The result obtained is:",result) 

Output

The result produced is as shown below −

The result obtained is: 3
python_maths.htm Previous Quiz Next Advertisements