Python change float to int

Python Float to Int

Summary: in this tutorial, you’ll learn how to convert a float to an integer.

Suppose that you have a float such as 20.3, and you want to convert it to an integer.

When you convert a float to an integer, you’ll have a data loss. For example, 20.3 may become 20 or 21 .

Python provides you with some functions in the math module for converting from a float to an int , including:

Truncation

The trunc(x) function returns the integer part of the number x . It ignores everything after the decimal point. For example:

from math import trunc print(trunc(12.2)) print(trunc(12.5)) print(trunc(12.7))Code language: Python (python)
12 12 12Code language: Python (python)

Similarly, the int() constructor accepts a float and uses truncation to cast a float to an int :

print(int(12.2)) print(int(12.5)) print(int(12.7))Code language: Python (python)

Floor

The floor(x) function returns the largest integer less than or equal to x . For example:

from math import floor print(floor(12.2)) print(floor(12.5)) print(floor(12.7))Code language: Python (python)
12 12 12Code language: Python (python)

The following shows how floor() function is applied to a positive number:

For positive numbers, floor(x) and trunc(x) return the same result. However, it’s not the case for negative numbers. For example:

The following picture shows how the floor() function is applied to a negative number:

from math import floor, trunc print(floor(-12.7)) print(trunc(-12.7))Code language: Python (python)
-13 -12Code language: Python (python)

The following picture illustrates the difference between the floor() and trunc() function when you apply them to a negative number:

Python float to int - floor vs trunc

Ceiling

The ceil(x) function returns the smallest integer greater than or equal to x . For example:

from math import ceil print(ceil(12.7))Code language: Python (python)
13Code language: Python (python)

The following illustrates how the ceil() function is applied to a positive number:

This example uses the ceil() function for negative numbers:

from math import ceil print(ceil(-12.7)) Code language: JavaScript (javascript)

The following illustrates how the ceil() function is applied to a negative number:

Summary

  • Convert a float to an int always results in a data loss.
  • The trunc() function returns the integer part of a number.
  • The floor() function returns the largest integer less than or equal to a number.
  • The ceil() function returns the smallest integer greater than or equal to a number.
Читайте также:  Php inspections ea extended php storm

Источник

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.
Читайте также:  Java 125 compass pools

Источник

Convert Float to Int in Python

Python Certification Course: Master the essentials

In Python, there are many situations where we have to convert float values to int values. Many approaches can be used for converting float to int values:

  • conversion using int() .
  • conversion using math.floor() and math.ceil() .
  • conversion using round() .
  • conversion using math.trunc() .

How to Convert Float to Int in Python?

We can convert a float value to an int using Type conversion in Python. Type Conversion is an explicit way of changing an operand to a certain type. Python comes with a few in-built methods to achieve this.

Displaying a floating value as a number or a percentage is a frequent application for converting a float to an integer. Integer values can be shown as such without affecting the actual data type because they are immutable. So when calculating efficiency, integer values are always preferred over floating-point values.

So for all this process, we use Python inbuilt functions for converting the float value to the integer values, for example int() function, etc.

Example: Let’s take a look at int values in Python

What is Float in Python?

The float type represents the floating point number in Python. A decimal point is used to separate the integer and fractional elements of the word «float,» which denotes real numbers.

Python uses 64-bit double-precision numbers to represent float values. Any floating-point number can have a maximum value of approximately 1.8 x 10^308. The Python string inf will indicate any number bigger than 1.8 x 10^308 . 77.44 , 42.3e19 , and -32.64+e12 are examples of floating point numbers.

Example: Let’s take a look at float values in Python

What is int in Python?

In Python is a whole number having no decimal points. Integer values can be of any size and can be negative or positive. Int Class defines Integer values. As integer does not consist of any decimal points and hence can extend up to the computer’s limit. However, you can subsequently change it. By default, Int is decimal, or base 10, although you can later convert it. Examples of some integer values are 20 , 1 , -10, etc.

Example: Let’s take a look at int value in Python

Methods to convert float to int in Python

For converting float to int in Python, we can use many methods. These methods use type conversion or inbuilt library functions enlisted below:

  • conversion using int()
  • conversion using math.floor() and math.ceil()
  • conversion using round()
  • conversion using math.trunc()

Method 1: Conversion using int()

Python has a variety of built-in methods that can make programming easier. One such method is int() , which quickly transforms any data type into an integer. The function trims the values after the decimal points and accepts a float-point variable as an argument. The result is an integer or whole number as the output. Let’s look at the syntax of int() method:

Читайте также:  What is magento framework in php

Return Value:

This method returns an integer value .

Example 1: Let’s take an example of how the int() method is used to convert the float value to int value:

Example 2: The int() function typically rounds off the results to the nearest integer before the decimal point. However, there are times when an exception occurs, so we cannot predict or define the behavior in those situations.

Output: It rounded out the value because there is an insignificant difference between 3.99999999999999999 and 4.0 .

Method 2: Conversion using math.floor() and math.ceil()

Use the floor() method and pass the value as the method’s parameter if you want to convert a float value to an int value that is no larger than the input. While using the ceil() method, you can change it to a value greater than the original.

Return Value: This method returns an integer value converting on the fact that ceil() is used or floor() is used.

Let’s take an example of how the floor() and ceil() method is used to convert the float value to int value:

Method 3: Conversion using round()

Using the round() method, a float value can be converted to an int value, which is the closest integer value. When there is an equal difference between smaller and larger values, the larger integer is chosen.

Return Value: This method returns an integer value rounding the given value to the nearest integer.

Let’s take an example of how the round() method is used to convert the float value to int value:

Method 4: Conversion using math.trunc()

Python has a built-in function called trunc() that helps in providing an integer value. The method takes the float-point value and ignores the remainder of the value. Let’s look at the example below to see how the trunc() method is used:

Return Value: This method returns an integer value removing the decimal part of the value.

Let’s take an example of how the trunc() method is used to convert the float value to int value:

Limitations for Converting Float to Int in Python

  • Although the int() method can be used for type conversions, it is not advised because there is a chance of data loss.
  • To prevent data loss, you can use the math module. If the function is not used properly, data loss does not change.

Conclusion

  • Many approaches can be used to convert float to int in Python.
  • We can convert a float value to an int using Type conversion.
  • The float type represents the floating point number in Python. A decimal point is used to separate the integer and fractional elements of the word «float,» which denotes real numbers.
  • In Python, Integer values can be of any size. Int values can be negative or positive. Int Class can define integer values.
  • These methods use type conversion or inbuilt library functions enlisted below:
    • conversion using int()
    • conversion using math.floor() and math.ceil()
    • conversion using round()
    • conversion using math.trunc()

    Источник

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