Python round float to int

Python Round to Int – How to Round Up or Round Down to the Nearest Whole Number

Ihechikara Vincent Abba

Ihechikara Vincent Abba

Python Round to Int – How to Round Up or Round Down to the Nearest Whole Number

When working with float values (numbers with decimal values) in our Python program, we might want to round them up or down, or to the nearest whole number.

In this article, we’ll see some built-in functionalities that let us round numbers in Python. And we’ll see how to use them with some examples.

We’ll start with the round() function. By default, it rounds a number to the nearest whole number. We’ll also see how to use the function’s parameters to change the type of result returned to us.

We’ll then talk about the math.ceil() and math.floor() methods which rounds up and rounds down a number to the nearest whole number/integer respectively. These two methods are from the built-in math module in Python.

Читайте также:  Html display error text

How to Use the round() Function to Round to the Nearest Whole Number

The round() function takes in two parameters. Here’s what the syntax looks like:

round(number, decimal_digits)

The first parameter – number – is the number we are rounding to the nearest whole number.

The second parameter – decimal_digits – is the number of decimals to be returned. The default value is 0.

In our first example, we’re using only one parameter – the number to be rounded, which is 2.56789 .

When we passed in the number variable to the round() function, it got rounded to the nearest whole number which is 3.

That’s how easy it is to use!

Now, let’s work with the second parameter.

x = 2.56789 print(round(x, 2)) # 2.57

The code above is similar to the last example except for the second parameter. We passed in a value of two. This will round the number to the nearest hundredth (two decimal places).

In our case, 2.57 was returned. That is, 2.56789 to 2.57.

Let’s see one last example to fully understand how the second parameter works.

x = 2.56789 print(round(x, 3)) # 2.568 

Now, we’ve made the second parameter 3. We’ll get the number rounded to the nearest thousandth (three decimal places).

The initial number – 2.56789 – was rounded to 2.568.

How to Use the math.ceil() Method to Round Up a Number to the Nearest Whole Number

The math.ceil() method simple takes in the number to be rounded up as its parameter. Here’s what the syntax looks like:

import math x = 5.57468465 print(math.ceil(x)) # 6 

In the code above, you’ll notice that we first imported the math module: import math . This give us access to all the methods provided by the module.

We created an x variable which has 5.57468465 as its value.

In order to round this number up to the nearest whole number, we passed in the number (in the x variable) to the math.ceil() method: math.ceil(x) .

The resulting value from this operation, as can be seen in the code above, is 6.

How to Use the math.floor() Method to Round Down a Number to the Nearest Whole Number

Just like we did in the last section, in order to use the math.floor() method, we must first import the math module.

Here’s the syntax for math.floor() method:

import math x = 5.57468465 print(math.floor(x)) # 5

As expected, we passed in the number to be rounded down to the math.floor() method: math.floor(x) . The x variable has the number 5.57468465 stored in it.

This number got rounded down to 5.

Conclusion

In this article, we talked about three built-in functionalities in Python that let us round numbers.

The round() function rounds a number to the nearest whole number.

The math.ceil() method rounds a number up to the nearest whole number while the math.floor() method rounds a number down to the nearest whole number. These two methods are both accessible through the math module.

With the examples given in each section, we were able to see how to use each functionality to obtain our desired result.

Источник

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.

Источник

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