Python float to int conversion

How to convert Float to int in Python?

In this short tutorial, we learn how to convert float to int Python. We look at all the various methods and discuss their pros and cons.

In case you are looking only for a safe solution to use in your application, I would recommend the second solution. However, if you are interested in the various methods available please follow along.

Table of Contents — Float to Int Python

Why convert float to int in Python?

Converting data types is a common practice and Python comes with a few in-built methods to achieve this. However, converting Float to int in Python must be dealt with more caution; we talk more about this later in the article.

A common use case of converting a float to int is to display a floating value as a number or a percentage. Since int values are immutable the values can be displayed as integer values without changing the original data type.

Читайте также:  Нужен ли сейчас питон

Apart from all this, when it comes to the efficiency of calculations, integer values are always preferred.

Solution 1 — Using int():

This method of converting float to int in Python uses the int() method to convert float to int. However, this method is prone to data loss and hence isn’t recommended.

The below example would give you a better understanding:

float_1 = 1.1 float_2 = 1.5 float_3 = 1.9 print(int(float_1)) #Output - 1 print(int(float_2)) #Output - 1 print(int(float_3)) #Output - 1 

As you can see, Python converts all the values into 1 which is an integer value. The data loss here may not seem significant unless you are dealing with currency values. In that case, the inefficient rounding of the float values can cause monetary losses.

Due to this, as a common practice, this method of conversion is not recommended.

Solution 2 — Using the math module:

The math module provides access to a lot of additional mathematical functions and among them, we make use of the ‘math.ceil’ and ‘math.floor’ functions.

The ‘math.ceil’ takes a floating value as a parameter and returns the smallest integer value that is greater than or equal to the argument.

Syntax of math.ceil:

The ‘math.floor’ function does the opposite of ‘ceil’ and returns the largest integer less than or equal to the argument.

Syntax of math.floor:

Since both methods return an integer value, this second method can also be used to convert float to Int in Python.

Parameter:

x — Required, the number that you are looking to convert

Code & Explanation:

Since both the above-mentioned methods are part of the math module, it needs to be imported before we use them.

import math float_1 = 1.1 float_2 = 1.5 float_3 = 1.9 print(math.floor(float_1)) print(math.ceil(float_1)) #Output - 1 #Output - 2 print(math.floor(float_2)) print(math.ceil(float_2)) #Output - 1 #Output - 2 print(math.floor(float_3)) print(math.ceil(float_3)) #Output - 1 #Output - 2 

As you can see in the above code snippet, the values can be converted based on your preference reducing data loss.

Limitations & Caveats — Float to Int Python

  • Although the int() method works, it is not recommended as there could be data loss. However, it can be used if you are just looking to understand the concept and not working on anything significant.
  • The math methods prevent data loss while converting float to int in Python; however, you could still cause a loss if the correct function is not used. I would recommend practicing it a few times to ensure you have a better understanding.

Источник

Convert float to int Python + Examples

To convert a float to int in python, we will use the built-in int() function. This function will return the integer number part.

num = 5.4 num = int(num) print(num)

You can refer to the below screenshot to see the output for how to convert float to int python.

How to convert float to int python

This is how to convert float to int in Python.

How to transform float to int Python

Now, we will see how to transform float to int python

In this example, we will use trunc() function. The trunc() function returns the integer part of the number. It ignores everything after the decimal points.

from math import trunc print(trunc(14.2))

You can refer to the below screenshot to see the output for how to transform float to int python.

How to transform float to int python

The above Python code we can use to transform float to int in Python.

How to convert float list to int in Python

Here, we will see how to convert float list to int in python

To convert float list to int in python we will use the built-in function int and it will return a list of integers.

num = [12.1, 14.2, 15.8, 17.4] print([int(num) for num in num])

You can refer to the below screenshot to see the output for how to convert float list to int in python.

How to convert float list to int in python

The above code, we can use to convert float list to int in Python.

How to convert float to whole number python

Let’s see how to convert float to whole number python

In this example, we will use the built-in round() function which will round up the value and returns the integer value.

You can refer to the below screenshot to see the output for how to convert float to whole number python.

How to convert float to whole number python

The above code, we can use to convert float to whole number in Python.

Python convert float to int ceil

Let us see how to convert float to int ceil.

In this example, we will use ceil() function which will rounds up the next full integer value.

from math import ceil print(ceil(15.7))

You can refer to the below screenshot to see the output for python convert float to int ceil.

Python convert float to int ceil

This is how to convert float to int ceil in Python.

Convert float array to int in python

Now, we will see how to convert float array to int in python.

To convert float array to int in python we will first import numpy as np and then we will use the function astype() and it will return the integer.

import numpy as np arr = np.array((1.4, 2.6, 3.1, 4.3)) arr = arr.astype(int) print(arr)

You can refer to the below screenshot to see the output for convert float array to int in python.

Convert float array to int in python

The above code, we can use to convert float array to int in python.

Convert float to int Python numpy

Here, we will see how to convert float to int python numpy.

In this example, we will import numpy as np and the built-in function astype() is used to convert float to int python numpy.

import numpy as np arr = np.array([[5.3, 6.5, 7.234], [30.3, 33.543, 35.995]]) arr = arr.astype(int) print(arr)

You can refer to the below screenshot to see the output for convert float to int python numpy.

Convert float to int python numpy

The above Python code, we can use to convert float to int Python numpy.

How to convert negative float value to int in python

Let’s see how to convert negative float value to int in python.

To convert the negative float value to int in python, we have to use the int() function. Also, we have to pass the float variable as the argument of the int() in python.

float_val = -12.8; print(int(float_val));

You can refer to the below screenshot to see the output for how to convert negative float value to int in python.

How to convert negative float value to int in python

This is how to convert negative float value to int in python.

How to convert float variable to int in python

Now, we will see how to convert float variable to int in python.

To convert float variable to int in python, we will use the int() function. Also, we have to pass the float variable as the argument of the int() in python, and to get the output use the print() function.

float_var = 18.8; print(int(float_var));

You can refer to the below screenshot to see the output for how to convert float variable to int in python.

How to convert float variable to int in python

The above code, we can use to convert float variable to int in python.

You may like the following Python tutorials:

In this Python tutorial, we have learned about how to convert float to int python. Also, we covered these below topics:

  • How to convert float to int python
  • How to transform float to int python
  • How to convert float list to int in python
  • How to convert float to whole number python
  • Python convert float to int ceil
  • Convert float array to int in python
  • Convert float to int python numpy
  • How to convert negative float value to int in python
  • How to convert float variable to int in python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

Источник

Оцените статью