• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Truncate Decimal from Number with Python math.trunc() Function

Truncate Decimal from Number with Python math.trunc() Function

June 9, 2022 Leave a Comment

The Python math module trunc() function returns the truncated integer part of a floating point number. You can use trunc() to truncate the decimal of a floating point number in Python.

import math

num = 1.53

print(math.trunc(num))

#Output:
1

When working with numbers, the ability to easily format and change the value of different numbers can be valuable.

One such situation is if you are working with decimal numbers or floating point numbers and want to truncate the decimal.

The Python math module has many powerful functions which make performing certain calculations in Python very easy.

The math module trunc() function allows you to truncate a decimal number and converts a float to an integer by simply removing the decimal portion of the number.

Below are some examples showing how you can truncate the decimal from a floating point number with the trunc() function.

import math

num_1 = 1.53
num_2 = -6.12
num_3 = 100.2341

print(math.trunc(num_1))
print(math.trunc(num_2))
print(math.trunc(num_3))

#Output:
1
-6
100

As you can see, trunc() simply removes the decimal portion from the numbers with decimals.

Truncating Decimal with int() and round() Functions in Python

There are two other ways you can truncate a decimal in Python. The Python int() function removes the decimal from a number in the same way as trunc().

import math

num_1 = 1.53
num_2 = -6.12
num_3 = 100.2341

print(int(num_1))
print(int(num_2))
print(int(num_3))

#Output:
1
-6
100

If you are looking to take into consideration the decimal portion part of the number, then you can use the Python round() function. round() rounds a number to the nearest integer.

Below shows you an example of how to remove the decimal from a number with round() in Python.

num_1 = 1.53
num_2 = -6.12
num_3 = 100.2341

print(round(num_1))
print(round(num_2))
print(round(num_3))

#Output:
2
-6
100

Hopefully this article has been useful for you to understand how to truncate a decimal and get the integer portion of a number in Python.

Other Articles You'll Also Like:

  • 1.  Intersection of Two Lists in Python
  • 2.  Get Name of Function in Python
  • 3.  Truncate String in Python with Slicing
  • 4.  pandas round – Round Numbers in Series or DataFrame
  • 5.  pandas Absolute Value – Get Absolute Values in a Series or DataFrame
  • 6.  Python issubset() Function – Check if Set is Subset of Another Set
  • 7.  pandas mad – Calculate Mean Absolute Deviation in Python
  • 8.  Check if Line is Empty in Python
  • 9.  Python not in – Check if Value is Not Included in Object
  • 10.  Using Python to Flatten Array of Arrays

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy