Skip to content

Instantly share code, notes, and snippets.

@jaymecd
Forked from jackiekazil/rounding_decimals.md
Created January 1, 2021 01:03
Show Gist options
  • Save jaymecd/5d528efce3dfa82216cfcef8457b3c93 to your computer and use it in GitHub Desktop.
Save jaymecd/5d528efce3dfa82216cfcef8457b3c93 to your computer and use it in GitHub Desktop.
How do I round to 2 decimals in python?

How do I round to 2 decimals?

In python, you have floats and decimals that can be rounded. If you care about the accuracy of rounding, use decimal type. If you use floats, you will have issues with accuracy.

All the examples use demical types, except for the original value, which is automatically casted as a float.

To set the context of what we are working with, let's start with an original value.

Original Value

print 16.0/7

Output: 2.2857142857142856

1: Round decimal using round()

from decimal import Decimal

# First we take a float and convert it to a decimal
x = Decimal(16.0/7)

# Then we round it to 2 places
output = round(x,2)
print output

Output: 2.29

2: Round decimal with super rounding powers

from decimal import Decimal, ROUND_HALF_UP
# Here are all your options for rounding:
# This one offers the most out of the box control
# ROUND_05UP       ROUND_DOWN       ROUND_HALF_DOWN  ROUND_HALF_UP
# ROUND_CEILING    ROUND_FLOOR      ROUND_HALF_EVEN  ROUND_UP

our_value = Decimal(16.0/7)
output = Decimal(our_value.quantize(Decimal('.01'), rounding=ROUND_HALF_UP))

print output

Output: 2.29

3: Round decimal by setting precision

# If you use deimcal, you need to import
from decimal import getcontext, Decimal

# Set the precision.
getcontext().prec = 3

# Execute 1/7, however cast both numbers as decimals
output = Decimal(16.0)/Decimal(7)

# Your output will return w/ 6 decimal places, which
# we set above.
print output

Output: 2.29

In example 3, if we set the prec to 2, then we would have 2.3. If we set to 6, then we would have 2.28571.

Solution

Which approach is best? They are all viable. I am a fan of the second option, because it offers the most control. If you have a very specific use case (i.e. 2010 WMATA practice rounding habits of up and down to the .05 depending on the fare), you may have to customize this part in your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment