• 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 / Using Python to Convert Float to Int

Using Python to Convert Float to Int

August 26, 2022 Leave a Comment

To convert a float to an integer in Python, the easiest way is with the int() function. int() will remove the decimal from the floating point number.

f = 1.2345

print(int(f))

#Output:
1

You can also use the math module trunc() function to convert a float variable to an integer.

import math

f = 1.2345

print(math.trunc(f))

#Output:
1

If you want to take into consideration the decimal of the number, you can use round() which will round up or down to the nearest integer.

f = 6.5432

print(round(f))

#Output:
7

When working with variables in programming, the ability to easily be able to convert variables into objects of a different variable type is valuable.

One such case is if you want to convert a float into an integer in Python.

Floats, or floating point numbers, have numbers after the decimal place.

If you want to convert the number into an integer, and remove the decimal portion of the float, the easiest way is with the int() function.

Below is a simple example of how you can use int() to convert a float into an integer in Python.

f = 1.2345

print(int(f))

#Output:
1

Using math Module trunc() to Convert Float to Integer in Python

Another method you can use to convert a float to an integer is with the math module trunc() function.

trunc() removes the decimal from a number and returns just the whole number piece of a number.

Below is a simple example showing you how to use trunc() to change a float to an integer in Python.

import math

f = 1.2345

print(math.trunc(f))

#Output:
1

Using round() to Convert Float to Integer in Python

One last function you can use to convert a float to an integer is with the built-in round() function.

round() will round a float up or down, depending on the decimal, to the nearest integer.

Below is an example of how you can use round() to turn a float into an integer in Python.

f = 6.5432

print(round(f))

#Output:
7

Hopefully this article has been useful for you to learn how to convert a float to an int in Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Count Items in List Matching Criteria
  • 2.  Skip Numbers in Python Range
  • 3.  Python sinh – Find Hyperbolic Sine of Number Using math.sinh()
  • 4.  Remove Brackets from String Using Python
  • 5.  Get Day of Year from Date in pandas DataFrame
  • 6.  Add Key Value Pair to Dictionary in Python
  • 7.  Remove Leading and Trailing Characters from String with strip() in Python
  • 8.  Create Empty List in Python
  • 9.  Python Even or Odd – Check if Number is Even or Odd Using % Operator
  • 10.  rfind Python – Find Last Occurrence of Substring in String

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