Which method returns the next integer greater than or equal to a given number?
When our Python program works with numerical values, every so often we have turn values with a fractional component into whole (integer) numbers. But how to do so? And what options does Python have? Let’s find out together. Show
IN THIS ARTICLE:
# Round numerical values up and down in PythonWhen we round values, we go from a numerical value with decimal places to a whole number. With this process we do lose some precision, but the rounded value is often much easier to read and interpret. Python has three ways to turn a floating-point value into a whole (integer) number:
If you just want a string or script output with a whole number, then a Python format string can perform that task too. That way you also don’t lose precision in the original value. Let’s see how these three approaches work. # Round values up and down: Python’s # Some numbers to round valueA = 3.14159265359 valueB = 1845.7409947 valueC = -100.95 valueD = 9.5432 valueE = 34.49953 # Round values to whole numbers roundA = round(valueA) roundB = round(valueB) roundC = round(valueC) roundD = round(valueD) roundE = round(valueE) # Output rounded values print("Value:".ljust(15), "Rounded:") print(str(valueA).ljust(15), roundA) print(str(valueB).ljust(15), roundB) print(str(valueC).ljust(15), roundC) print(str(valueD).ljust(15), roundD) print(str(valueE).ljust(15), roundE) 5 functionTo round floating-point values up and down we use Python’s 5 function (Lutz, 2013; Python Docs, n.d. a). There are two ways to use this function. The first option is to round values to a certain number of decimals. The other option turns a floating-point value into a whole number.To do that latter, we call 5 with one argument: the value to turn into an integer. For example:
The 5 function rounds values of 6 towards an even integer (Python Docs, n.d. a). So 6 is round up for positive values and round down for negative values.For instance, both 8 and 9 return 0, while 1 gives 2 and 3 gives 4. This Python behaviour is a bit different from how rounding usually goes.When we give 5 an integer, the function simply returns that whole number. There’s no error in that case, and so we don’t have to check if the function’s argument has a fractional value. It does need to be a number though; string values are not allowed in 5.# Example: round Python numbers to the nearest full integerTo see how the 5 function works in practice, let’s consider the following mini-program:
Here we first make five variables with floating-point values. Some have a lot of decimal places and others just a few. Then we do some rounding. For that we call the 5 function and provide one argument: the value to round. We store the rounded integers in the variables 9 through 0.Next we output the results with the 1 function. For each variable we display the original value (e.g., 2) and its rounded result ( 9). With the 4 string method we justify that first value to the left. That aligns the values for a prettier output.Here’s how the rounded values look:
FURTHER READING
# Round down to the next integer: Python’s # Some numbers to round valueA = 3.14159265359 valueB = 1845.7409947 valueC = -100.95 valueD = 9.5432 valueE = 34.49953 # Round values to whole numbers roundA = round(valueA) roundB = round(valueB) roundC = round(valueC) roundD = round(valueD) roundE = round(valueE) # Output rounded values print("Value:".ljust(15), "Rounded:") print(str(valueA).ljust(15), roundA) print(str(valueB).ljust(15), roundB) print(str(valueC).ljust(15), roundC) print(str(valueD).ljust(15), roundD) print(str(valueE).ljust(15), roundE) 6 functionThe 6 function returns the floor value of its argument, which is the nearest integer less than or equal to that argument’s value (Python Docs, n.d. b).That sounds abstract, but is just another way of saying that 6 rounds down to the next whole number. So 9 becomes 0 and 1 is turned into 2. And since the function rounds down to a smaller value, 3 becomes 4.Here’s a quick example of the 6 function:
6 only accepts one argument: the value to round down. With a small custom function we can also round down to a number of decimal places. See round down to a specific decimal amount for more.# Example: round values down to the next full integerTo explore how the 6 function works in practice, let’s examine the following Python program:
We first import the 8 module. That makes it possible to use the 6 function. Then we make five variables, 2 through 1. Each holds a floating-point value.Next we round those values down. For that we call the 6 function on each variable. We store the outcome in new variables ( 9 through 0).The program’s third part outputs the variables with Python’s 1 function. Here each 1 statement displays the original value and its rounded down version. Here’s how that looks:
READ MORE
# Round up to the next integer: Python’s # Some numbers to round valueA = 3.14159265359 valueB = 1845.7409947 valueC = -100.95 valueD = 9.5432 valueE = 34.49953 # Round values to whole numbers roundA = round(valueA) roundB = round(valueB) roundC = round(valueC) roundD = round(valueD) roundE = round(valueE) # Output rounded values print("Value:".ljust(15), "Rounded:") print(str(valueA).ljust(15), roundA) print(str(valueB).ljust(15), roundB) print(str(valueC).ljust(15), roundC) print(str(valueD).ljust(15), roundD) print(str(valueE).ljust(15), roundE) 7 functionThe 7 function returns the ceiling of its argument, which is the nearest integer greater than or equal to that argument’s value (Python Docs, n.d. b).That’s just a way of saying that 7 rounds up to a whole number: 1 becomes 2 and 3 gets turned into 4. And because the function rounds up to a greater value, 5 becomes 4.Here’s a quick example of 7:
Here’s a way to remember the difference between 6 and 7. As you know, each floating-point value lies between two consecutive integers. 0, for instance, is between 1 and 2.Now the “ceiling” is the higher endpoint of this interval. So 7 returns 2. The lower start point of that integer interval is called the “floor”. So 6 returns 12.Python’s 7 function always rounds up to a full integer. But with a small custom function we can also round up to a number of decimal places. See round Python values to decimal places for how.# Example: round Python values up to whole numbersLet’s see how 7 works in practice. This example program rounds several floating-point values up to a whole number:
We first import the 8 module. That makes the 7 function available. Then we make five different variables, named 2 through 1. Each has a number with several decimal places.The next part rounds those values up to a full integer. To make that happen we call the 7 function on each variable. We put the values returns by that function in new variables, 9 through 0.The third code segment has the 1 function output both the original and rounded value. Here’s what that displays:
LEARN MORE
# Round all values in a Python list or arrayOf course there are also situations where we have a bunch of values to round, rather than a single value. There are two main ways to do that: with a list comprehension or 8 loop. Let’s see.# Round Python values with a list comprehensionWhen we have a sequence of floating-point values, one way to round them is with a list comprehension. That requires just a bit of code and runs efficiently. Here’s a mini-program that does just that:
First we import the 8 module. That makes the 7 and 6 rounding functions available. Then we make a list named 01, which holds several floating-point values.To round those values to whole numbers we make three list comprehensions. The first one executes 5 for each list value. The other two execute 7 and 6 functions on the list values.We generate the values that those functions use with an in-line 8 loop: 06. This takes one value from the 01 list at a time, and makes it available through the 08 variable.Those list comprehensions generate new lists. We assign those to the 09, 10, and 11 variables.The last part of the program outputs the original list and the three rounded ones. Here’s how that looks: 0In the above example we kept the original list. If you don’t need to retain those values, you can also overwrite the original list with rounded values. Here’s how a list comprehension does that: 1# Round all values with Python’s # Some numbers to round valueA = 3.14159265359 valueB = 1845.7409947 valueC = -100.95 valueD = 9.5432 valueE = 34.49953 # Round values to whole numbers roundA = round(valueA) roundB = round(valueB) roundC = round(valueC) roundD = round(valueD) roundE = round(valueE) # Output rounded values print("Value:".ljust(15), "Rounded:") print(str(valueA).ljust(15), roundA) print(str(valueB).ljust(15), roundB) print(str(valueC).ljust(15), roundC) print(str(valueD).ljust(15), roundD) print(str(valueE).ljust(15), roundE) 8 loopOf course we can also round list or array values with a regular 8 loop. This requires a bit more code than a list comprehension, but makes it easier to perform additional operations on each element. Plus a 8 loop is easier to read in complex situations.Here’s how a Python program rounds values inside a 8 loop: 2We again first import the 8 module to be able to use 7 and 6. Then we make a list ( 01) with floating-point values.Then we make three initially empty lists: 09, 10, and 11. These are going to hold our rounded values.To fill those lists we make a Python 8 loop. This loop goes through all elements in the 01 list. During each loop cycle the 08 variable holds a single element from that list.Inside the loop we call the 26 method on each of the three new lists. That way we add a new element to them. The value we add each pass through the loop is the 08 variable rounded with 5, 7, and 6. After this loop is done, each of those three lists has a rounded value from the original list.The program ends with several 1 statements. That displays the original list and its rounded derivatives. Here’s what that output looks like: 3By the way, you don’t have to make a new list when you round values. If you’re fine with losing the original data, you can also overwrite the existing list. Doing so is easy when you combine a 8 loop with the 33 function: 4READ MORE
# SummaryPython has three ways to round a floating-point value to a whole number. The 5 function rounds a value up or down. A decimal digit of 6 has Python round towards an even integer. That makes it round up for positive values and down for negative ones.The 6 function, on the other hand, always rounds down to the nearest full integer. 7 does the opposite. That function always rounds up to a whole number.All three functions work in the same way: provide the function with one argument, which is the floating-point value to round. ReferencesLutz, M. (2013). Learning Python (5th Edition). Sebastopol, CA: O’Reilly Media. Python.org (n.d. a). Built-in Functions. Retrieved on November 8, 2019, from https://docs.python.org/3/library/functions.html Python.org (n.d. b). math — Mathematical functions. Retrieved on October 22, 2019, from https://docs.python.org/3.8/library/math.html Published December 20, 2019. # Related Python tutorials
|