• 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 / Remove Decimal from Float in Python

Remove Decimal from Float in Python

June 9, 2022 Leave a Comment

There are three ways you can remove the decimal from a floating point number in Python. Depending on what you want to accomplish, any of these ways might work for you.

The first way is to use the Python round() function.

num = 1.53

print(round(num))

#Output:
2

You can also use the Python math module trunc() function.

import math

num = 1.53

print(math.trunc(num))

#Output:
1

One last way is to use the Python int() function.

num = 1.53

print(int(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 remove the decimal.

In Python, there are a few different ways to remove the decimal numbers.

Depending on what you want to do, you can use any of these methods to remove the decimals.

For example, the Python int() function converts a float to an integer and simply removes the decimal portion of the number. The math module trunc() function has the same behavior.

Below is an example showing how you can remove the decimal from a floating point number with the int() and trunc() functions.

import math

num_1 = 1.53
num_2 = -6.12
num_3 = 100.2341

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

#Output:
1
1
-6
-6
100
100

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

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 remove decimals from numbers in Python.

Other Articles You'll Also Like:

  • 1.  Using Python turtle Module to Draw Square
  • 2.  PROC LIFETEST Equivalent in Python
  • 3.  pandas groupby size – Get Number of Elements after Grouping DataFrame
  • 4.  Using Python to Count Items in List Matching Criteria
  • 5.  Get List of Letters in Alphabet with Python
  • 6.  Using Selenium to Get Text from Element in Python
  • 7.  Check if pandas DataFrame is Empty in Python
  • 8.  Run Something Every 5 Seconds in Python
  • 9.  How to Check If Value is in List Using Python
  • 10.  Check if String Does Not Contain Substring in Python

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