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 Python
  • 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 function
    • Example: round Python numbers to the nearest full integer
  • 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 function
    • Example: round values down to the next full integer
  • 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 function
    • Example: round Python values up to whole numbers
  • Round all values in a Python list or array
    • Round Python values with a list comprehension
    • 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 loop
  • Summary

# Round numerical values up and down in Python

When 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:

  • The built-in
    # 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 function rounds values up and down.
  • The
    # 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 function rounds down to the next full integer.
  • The
    # 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 function rounds up to the next full integer.

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 function

To round floating-point values up and down we use 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 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

# 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 with one argument: the value to turn into an integer. For example:

round(5.4598)
# Returns: 5

The

# 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 function rounds values of
Value:          Rounded:
3.14159265359   3
1845.7409947    1846
-100.95         -101
9.55432         10
34.49953        34
6 towards an even integer (Python Docs, n.d. a). So
Value:          Rounded:
3.14159265359   3
1845.7409947    1846
-100.95         -101
9.55432         10
34.49953        34
6 is round up for positive values and round down for negative values.

For instance, both

Value:          Rounded:
3.14159265359   3
1845.7409947    1846
-100.95         -101
9.55432         10
34.49953        34
8 and
Value:          Rounded:
3.14159265359   3
1845.7409947    1846
-100.95         -101
9.55432         10
34.49953        34
9 return
import math

math.floor(12.75)
# Returns: 12
0, while
import math

math.floor(12.75)
# Returns: 12
1 gives
import math

math.floor(12.75)
# Returns: 12
2 and
import math

math.floor(12.75)
# Returns: 12
3 gives
import math

math.floor(12.75)
# Returns: 12
4. This Python behaviour is a bit different from how rounding usually goes.

When we give

# 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 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
# 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.

# Example: round Python numbers to the nearest full integer

To see how the

# 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 function works in practice, let’s consider the following mini-program:

# 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)

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

# 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 function and provide one argument: the value to round. We store the rounded integers in the variables
import math

math.floor(12.75)
# Returns: 12
9 through
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
0.

Next we output the results with the

import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
1 function. For each variable we display the original value (e.g.,
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
2) and its rounded result (
import math

math.floor(12.75)
# Returns: 12
9). With the
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
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:

Value:          Rounded:
3.14159265359   3
1845.7409947    1846
-100.95         -101
9.55432         10
34.49953        34

FURTHER READING

  • Round to a number of decimal places with
    # 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

# 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 function

The

# 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 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

# 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 rounds down to the next whole number. So
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
9 becomes
11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
0 and
11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
1 is turned into
11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
2. And since the function rounds down to a smaller value,
11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
3 becomes
11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
4.

Here’s a quick example of the

# 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 function:

import math

math.floor(12.75)
# Returns: 12

# 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 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 integer

To explore how the

# 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 function works in practice, let’s examine the following Python program:

import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)

We first import the

11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
8 module. That makes it possible to use the
# 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 function. Then we make five variables,
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
2 through
import math

math.ceil(12.45)
# Returns: 13
1. Each holds a floating-point value.

Next we round those values down. For that we call the

# 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 function on each variable. We store the outcome in new variables (
import math

math.floor(12.75)
# Returns: 12
9 through
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
0).

The program’s third part outputs the variables with Python’s

import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
1 function. Here each
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
1 statement displays the original value and its rounded down version. Here’s how that looks:

11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10

READ MORE

  • Round down to a certain number of decimal places with
    # 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

# 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 function

The

# 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 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

# 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 rounds up to a whole number:
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values up to the nearest full integer
roundA = math.ceil(valueA)
roundB = math.ceil(valueB)
roundC = math.ceil(valueC)
roundD = math.ceil(valueD)
roundE = math.ceil(valueE)

# Output the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
1 becomes
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values up to the nearest full integer
roundA = math.ceil(valueA)
roundB = math.ceil(valueB)
roundC = math.ceil(valueC)
roundD = math.ceil(valueD)
roundE = math.ceil(valueE)

# Output the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
2 and
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values up to the nearest full integer
roundA = math.ceil(valueA)
roundB = math.ceil(valueB)
roundC = math.ceil(valueC)
roundD = math.ceil(valueD)
roundE = math.ceil(valueE)

# Output the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
3 gets turned into
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values up to the nearest full integer
roundA = math.ceil(valueA)
roundB = math.ceil(valueB)
roundC = math.ceil(valueC)
roundD = math.ceil(valueD)
roundE = math.ceil(valueE)

# Output the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
4. And because the function rounds up to a greater value,
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values up to the nearest full integer
roundA = math.ceil(valueA)
roundB = math.ceil(valueB)
roundC = math.ceil(valueC)
roundD = math.ceil(valueD)
roundE = math.ceil(valueE)

# Output the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
5 becomes
11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
4.

Here’s a quick example of

# 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:

import math

math.ceil(12.45)
# Returns: 13

Here’s a way to remember the difference between

# 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 and
# 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. As you know, each floating-point value lies between two consecutive integers.
11.282985 rounded = 12
19.2545879 rounded = 20
0.50000001 rounded = 1
34.6403001 rounded = 35
-9.9121138 rounded = -9
0, for instance, is between
11.282985 rounded = 12
19.2545879 rounded = 20
0.50000001 rounded = 1
34.6403001 rounded = 35
-9.9121138 rounded = -9
1 and
11.282985 rounded = 12
19.2545879 rounded = 20
0.50000001 rounded = 1
34.6403001 rounded = 35
-9.9121138 rounded = -9
2.

Now the “ceiling” is the higher endpoint of this interval. So

# 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 returns
11.282985 rounded = 12
19.2545879 rounded = 20
0.50000001 rounded = 1
34.6403001 rounded = 35
-9.9121138 rounded = -9
2. The lower start point of that integer interval is called the “floor”. So
# 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 returns 12.

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 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 numbers

Let’s see how

# 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 works in practice. This example program rounds several floating-point values up to a whole number:

import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values up to the nearest full integer
roundA = math.ceil(valueA)
roundB = math.ceil(valueB)
roundC = math.ceil(valueC)
roundD = math.ceil(valueD)
roundE = math.ceil(valueE)

# Output the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)

We first import the

11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
8 module. That makes the
# 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 function available. Then we make five different variables, named
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
2 through
import math

math.ceil(12.45)
# Returns: 13
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

# 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 function on each variable. We put the values returns by that function in new variables,
import math

math.floor(12.75)
# Returns: 12
9 through
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
0.

The third code segment has the

import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
1 function output both the original and rounded value. Here’s what that displays:

11.282985 rounded = 12
19.2545879 rounded = 20
0.50000001 rounded = 1
34.6403001 rounded = 35
-9.9121138 rounded = -9

LEARN MORE

  • Round up to a certain number of decimal places with
    # 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

# Round all values in a Python list or array

Of 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

# 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 loop. Let’s see.

# Round Python values with a list comprehension

When 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:

import math

# Some random values
values = [
    3.46410162, 6.70820393, 11.04536102,
    15.29705854, 21.21320344, 31.4960315
]

# Generate new lists with values rounded
valuesRounded = [round(number) for number in values]
valuesRoundUp = [math.ceil(number) for number in values]
valuesRoundDown = [math.floor(number) for number in values]

# Output data
print("Original values:\n", values)
print("Rounded:\n", valuesRounded)
print("Rounded up to next integer:\n", valuesRoundUp)
print("Rounded down to next integer:\n", valuesRoundDown)

First we import the

11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
8 module. That makes the
# 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 and
# 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 rounding functions available. Then we make a list named
# 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)
01, which holds several floating-point values.

To round those values to whole numbers we make three list comprehensions. The first one executes

# 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 for each list value. The other two execute
# 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 and
# 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 functions on the list values.

We generate the values that those functions use with an in-line

# 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 loop:
# 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)
06. This takes one value from the
# 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)
01 list at a time, and makes it available through the
# 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)
08 variable.

Those list comprehensions generate new lists. We assign those to the

# 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)
09,
# 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)
10, and
# 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)
11 variables.

The last part of the program outputs the original list and the three rounded ones. Here’s how that looks:

# 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)
0

In 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:

# 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)
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 loop

Of course we can also round list or array values with a regular

# 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 loop. This requires a bit more code than a list comprehension, but makes it easier to perform additional operations on each element. Plus a
# 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 loop is easier to read in complex situations.

Here’s how a Python program rounds values inside a

# 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 loop:

# 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)
2

We again first import the

11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
8 module to be able to use
# 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 and
# 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. Then we make a list (
# 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)
01) with floating-point values.

Then we make three initially empty lists:

# 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)
09,
# 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)
10, and
# 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)
11. These are going to hold our rounded values.

To fill those lists we make a Python

# 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 loop. This loop goes through all elements in the
# 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)
01 list. During each loop cycle the
# 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)
08 variable holds a single element from that list.

Inside the loop we call the

# 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)
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
# 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)
08 variable rounded with
# 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,
# 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, and
# 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. After this loop is done, each of those three lists has a rounded value from the original list.

The program ends with several

import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
1 statements. That displays the original list and its rounded derivatives. Here’s what that output looks like:

# 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)
3

By 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

# 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 loop with the
# 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)
33 function:

# 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)
4

READ MORE

  • Round Python values to a certain number of decimal places
  • Truncate Python floating-point values to a whole number
  • Truncate Python values to a certain number of decimal digits

# Summary

Python has three ways to round a floating-point value to a whole number. The

# 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 function rounds a value up or down. A decimal digit of
Value:          Rounded:
3.14159265359   3
1845.7409947    1846
-100.95         -101
9.55432         10
34.49953        34
6 has Python round towards an even integer. That makes it round up for positive values and down for negative ones.

The

# 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 function, on the other hand, always rounds down to the nearest full integer.
# 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 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.

References

Lutz, 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.

  • How to check if a number is a perfect cube in Python?

    A perfect cube is a number that, when multiplied with itself twice, returns a integer. This article uses Python code to find those perfect cubes.

  • How to calculate the square root in Python?

    Python can calculate the square root of a number in three ways:

    # 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)
    
    38,
    # 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)
    
    39, or
    # 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)
    
    40. This article explains each with easy-to-follow code.

  • How to get the absolute value of numbers in Python?

    An absolute value is a number’s non-negative value. Python code gets those with the

    # 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)
    
    41 function. This article explains how (including lists & arrays).

  • How to truncate numbers to a number of decimal places in Python?

    When we truncate a number, we throw away its fractional value. But with a custom function Python can also truncate to a certain amount of decimal places.

  • How to square a number in Python?

    A square is a number multiplied by itself. In Python

    # 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)
    
    40 and
    # 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)
    
    39 square by raising to the power 2, while
    # 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)
    
    44 can multiply a value with itself.

    Which method returns the smallest integer greater than or equal to a number?

    ceil method is used to return the smallest integer greater than or equal to a given number. The ceil() is always used as Math.

    Which function is used to return the integer value that is greater than or equal to a number?

    Definition and Usage The CEIL() function returns the smallest integer value that is bigger than or equal to a number.

    Which method returns the next integer less than or equal to a given number?

    floor() method may be used to get the nearest integer that is less than or equal to a given value.

    Which function returns nearest integer value which is greater than or equal to the argument passed to this function?

    Ceil function is a predefined function of math. h header file. It returns the nearest integer number, which is greater than or equal to the number passed as an argument in it. For example, we pass the float number 3.4, and the ceil() function returns the greatest number 4.