How do you round to a decimal in python?

Python has several options to round to a whole number. But what if we want to round to a number of decimal digits, like 2 decimal places? Let’s see how we do that.

IN THIS ARTICLE:

  • Round Python values to two decimals places [and more]
  • Round decimal places up and down: round[]
    • Example: round decimal digits up and down
  • Round decimal places up in Python
    • Example: round up to 2 decimal places
  • Round decimal places down in Python
    • Example: round down to 2 decimal places
  • Round decimal places of Python lists and arrays
    • Rounds lists to decimal places with a list comprehension
    • Round lists to decimal places with Python’s for loop
    • Round Python arrays to 2 decimal places
    • Round NumPy arrays to two decimal places
  • Summary

# Round Python values to two decimals places [and more]

Though rounding to a whole number is helpful, there are still plenty of situations in which we need to round to a certain number of decimal places. For example, currency is best reported with 2 decimal digits. And error levels and significance often use more digits, like 5.

Python has several ways to round decimal digits:

  • The round[] function rounds decimal places up and down. This makes 4.458 into 4.46 and 8.82392 into 8.82.
  • To round decimal places up we have to use a custom function. That way 8.343 becomes 8.35.
  • To round decimal places down we use a custom function. That turns 2.348 into 2.34.

Let’s look at each approach and the Python code they require.

# Round decimal places up and down: round[]

With Python’s round[] function we can round decimal places up and down. The function needs two arguments for that. The first is the value to round. The second is the number of digits after the decimal point [.] to round to [Lutz, 2013; Sweigart, 2015].

So to round to 2 decimal places we do:

round[12.54673, 2]
# Returns: 12.55

# Example: round decimal digits up and down

To see how round[] works in practice, let’s explore the following mini-program:

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]

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

This code first makes five floating-point variables. Each has several numbers after the decimal point [.]. We store their values in variables valueA through valueE.

Then we round each to a number of decimal places. We call the round[] function with two arguments. The first is the variable we just made. The second the number of decimals to round to. Here we use values 5 through 1 for that. We store the rounded values in new variables [roundA through roundE].

Then Python’s print[] function displays both the original and rounded value. With the ljust[] string method we align the original value to the left. That makes for a nice table-like output. As we can see from the output, each value is rounded correctly:

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5

# Round decimal places up in Python

Another option is to always round a number’s decimal digits up. That’s for instance what companies do: rather than round prices up or down, values like 4.5421 are instead always rounded up to $4.55.

Python, however, doesn’t have a built-in function for that. But we can make our own and leverage the math.ceil[] function. When we do, using the function looks like this:

round_decimals_up[8.343]
# Returns: 8.35

Here’s how we code that round_decimals_up[] function:

import math


def round_decimals_up[number:float, decimals:int=2]:
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance[decimals, int]:
        raise TypeError["decimal places must be an integer"]
    elif decimals 

Chủ Đề