Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
For a long time, Python has been a very well-liked programming language. This is primarily because of its adaptability and simple syntax. Python also has amazing methods that you can use in your Project or learning. One of the amazing methods is the math.trunc() method that comes under the math module.
This article will guide you how you can use the math.trunc() method. You will learn its syntax and implementation through the examples.
So, let us start:
math.trunc() Method
The “math.trunc()” method is a part of the math module in python. There are a number of modules that exist in python, one of them is the math module.
The math module in Python is a standard library module that provides a wide range of mathematical functions and constants. It allows you to perform various mathematical operations and calculations easily in your Python programs.
The math module is part of the Python Standard Library, so you don't need to install any additional packages to use it.
Let us now talk about the math.trunc() method:
Description: This method, as the name suggests, truncates a floating point number towards zero and returns the integer part of the number.
Let us now discuss its syntax:
Syntax of math.trunc() Method
Below is the syntax of math.trunc() method:
>> math.trunc( num )
Where, num is the number you want to truncate towards the zero.
Time for Examples
Let us see some examples:
Example 1
Python
Python
import math #Your number num = 3.11
#truncate your number towards zero truncated_num = math.trunc(num)
#print the result print(f"The truncated number of {num} is {truncated_num}")
You can also try this code with Online Python Compiler
In the example above as you can see the method has truncated 3.11 to 3.
Note: The math.trunc() method doesn’t round the number, rather it removes the decimal part and returns the Integer part. This is often used when you want to discard the fractional part of a floating-point number without rounding up or down.
We have tried passing the positive number. Let us now pass the negative number and see what it returns.
Example 2
Python
Python
import math
#Negative number num = -3.11
#truncate your number towards zero truncated_num = math.trunc(num)
#print the result print(f"The truncated number of {num} is {truncated_num}")
You can also try this code with Online Python Compiler
There are many use cases of this method. Some of them are listed below:
Removal of Decimal Part
It plays a crucial role in removing the decimal part from your number. It may be used in further analysis and development
Converting Floating Point numbers into Integers
Using math.trunc() can be treated as converting the floating point numbers to Integers.
Let us say, you have an array of floating point numbers, you can use this method to convert an array of floating point numbers to an array of Integers.
Example:
Python
Python
import math
numbers = [2.71828, 3.14159, 1.61803]
print("Array of floating-point numbers") for number in numbers: print(number)
truncated_numbers = [math.trunc(x) for x in numbers]
print("Array of truncated numbers (Integers)") for number in truncated_numbers: print(number)
You can also try this code with Online Python Compiler
The math module in Python is a standard library module that provides a wide range of mathematical functions and constants. It allows you to perform various mathematical operations and calculations easily in your Python programs.
How can we use the modules in Python?
We can use the modules by using the import keyword followed by the module name to import the module into our current project.
How can we create a module in Python?
We can simply save our code file with the .py extension to make it a module.
What makes Python so popular?
Python is one of the most loved user-friendly programming languages available because of its simple syntax and emphasis on natural language.
Conclusion
To wrap up the article, we have extensively discussed the Python math.trunc() method in detail. We have looked at its examples for different scenarios. We have also discussed its use cases.