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.
SyntaxFollowing 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 ValueThe method returns an integer, which represents the truncated value of the given value "x".
Example 1In 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: 10Example 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: -10Example 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: 15Example 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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4