Table of contents
1.
Introduction
2.
math.trunc() Method 
2.1.
Syntax of math.trunc() Method
3.
Time for Examples 
3.1.
Example 1
3.2.
Python
3.3.
Example 2
3.4.
Python
3.5.
Example 3
3.6.
Python
4.
Uses of math.trunc() Method 
4.1.
Removal of Decimal Part 
4.2.
Converting Floating Point numbers into Integers
4.3.
Python
4.4.
Mathematical Operations
4.5.
Python
5.
Frequently asked questions
5.1.
What is a math module in Python?
5.2.
How can we use the modules in Python?
5.3.
How can we create a module in Python?
5.4.
What makes Python so popular?
6.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Python math.trunc() Method

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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. 

Python math.trunc() Method

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
Run Code


Output 

Python math.trunc() Method

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
Run Code


Output

Python math.trunc() Method

Woah! It worked for a negative number as well. 

Important: It will throw the TypeError if the passed parameter is not a number. 

Let us see: 

Example 3

  • Python

Python

import math

# num is not a number
num = "Three"
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
Run Code


Output 

Python math.trunc() Method

Let us now see the uses of math.trunc() method: 

Uses of math.trunc() Method 

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
Run Code


Output 

Mathematical Operations

You can use math.trunc() as part of more complex mathematical operations where you need to extract the integer part of intermediate results.

Example

  • Python

Python

import math

result = math.sqrt(10) * 3.5
truncated_result = math.trunc(result)
print(f"The truncated number of {result} is {truncated_result}")
You can also try this code with Online Python Compiler
Run Code


Output 

Frequently asked questions

What is a math module in Python?

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. 

After reading the article, do you feel more excited to read/explore more articles on Python? Don't worry; Coding Ninjas has you covered. See Exception & Errors in PythonShort-Circuiting in PythonLambda Function in Python, and File Handling in Python.

Refer to our Guided Path on CodingNinjas Studio to upskill yourself in Python programming language, Competitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass