0% found this document useful (0 votes)
196 views7 pages

How Do I Round To 2 Decimals in Python - GitHub

Uploaded by

vaskore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views7 pages

How Do I Round To 2 Decimals in Python - GitHub

Uploaded by

vaskore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

10/19/2020 How do I round to 2 decimals in python?

· GitHub

Instantly share code, notes, and snippets.

jackiekazil / rounding_decimals.md
Last active 25 days ago

Star

Code Revisions 8 Stars 111 Forks 19

How do I round to 2 decimals in python?

rounding_decimals.md

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
https://gist.github.com/jackiekazil/6201722 1/7
10/19/2020 How do I round to 2 decimals in python? · GitHub

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.

https://gist.github.com/jackiekazil/6201722 2/7
10/19/2020 How do I round to 2 decimals in python? · GitHub

gonvaled commented on Sep 16, 2015

See here for an alternative using __format__

Serpens66 commented on Oct 11, 2015

I think you have to use the Decimal() with strings ?


Because
Decimal(1.1)+Decimal(2.2) == 3.300000000000000266453525910
While
Decimal(repr(1.1))+Decimal(repr(2.2)) == 3.3

Or am I wrong?

The-Loeki commented on Dec 7, 2015

Decimal(float) will give you the decimal representation of a floating point number. What you're seeing is
the entire raison d'etre for Decimals; floats are binary approximations of (supposedly really large) numbers,
converting them to Decimal (or trying to round them etc.) will expose that approximation, and if you're not
aware of those internals you'll run into these kinds of suprises.

See for example https://docs.python.org/3/tutorial/floatingpoint.html

JamieMcNaught commented on Jun 20, 2016

I don't think rounding by setting the precision is viable as it takes into account the digits before the decimal
point as well as after (hence why set to 3 and not 2). In you example if you do:

from decimal import getcontext, Decimal


getcontext().prec = 3
output = Decimal(160.0)/Decimal(7)
print output
22.9

Which I don't think is what you want? You can of course set prec to 4, but that's not really a solution.

rpxz commented on Jun 24, 2016


https://gist.github.com/jackiekazil/6201722 3/7
10/19/2020 How do I round to 2 decimals in python? · GitHub

if i'll write
v= Decimal(x)
x is a float() type ,then can i use round(v,2)?

SeanTankGarvey commented on Jul 23, 2017

@rpxz if you have added the line above it that says:


from decimal import Decimal

LePingKYXK commented on Jul 30, 2017 • edited

Hello, thank you very much for sharing.


I found a strange result, maybe it is a bug in the decimal module. The code is shown below.

>>>from decimal import Decimal, ROUND_HALF_UP


>>>val = Decimal(1.45)
>>>Decimal(val.quantize(Decimal('0.1'), rounding=ROUND_HALF_UP))

Decimal('1.4')

The correct result should be Decimal('1.5').


By the way, I am using Python 3.5.

dualbus commented on Aug 6, 2017

You forgot to pass 1.45 as a string:

Python 3.5.4rc1 (default, Jul 25 2017, 08:53:34)


[GCC 6.4.0 20170704] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal, ROUND_HALF_UP
>>> val = Decimal('1.45')
>>> Decimal(val.quantize(Decimal('0.1'), rounding=ROUND_HALF_UP))
Decimal('1.5')
>>> Decimal(1.45) - Decimal('1.45')
Decimal('-4.440892098500626161694526672E-17')

Which is mentioned above in https://gist.github.com/jackiekazil/6201722#gistcomment-1641922

https://gist.github.com/jackiekazil/6201722 4/7
10/19/2020 How do I round to 2 decimals in python? · GitHub

FirasBayazed commented on Oct 8, 2017

https://www.youtube.com/watch?v=l1coJkTxhBY

if anyone could understand this videos can you please write for me the steps and the code so I can type it in
paython interpreter
I will be very thankfull

nvictor commented on Feb 16, 2018

nice

nagimov commented on Mar 6, 2018

3a. Round decimals by setting precision without Decimal

to round float to 3 significant figures without dealing with Decimal module:

>>> '%s' % float('%.3g' % 3.141592654)


'3.14'

%s required to prevent python from printing float in e- scientific notation

Ref: https://stackoverflow.com/questions/3410976/how-to-round-a-number-to-significant-figures-in-python

Baahcy commented on Sep 5, 2018

for Python 3

Depending on the number you want to round


Kindly use, Example

The codes start below

a = 16 / 7
print ("%.1f" % a)

https://gist.github.com/jackiekazil/6201722 5/7
10/19/2020 How do I round to 2 decimals in python? · GitHub

Explaining

#The '1 explains where it should starts from


just after the decimal point.
#The 'a' represents the value you assigned it to
If you want it to round on the second, use 2
Like this ----below is the code

print ("%.2f" % a)

Ankit-Chauhan10 commented on Oct 9, 2018

how to get 3.25, 3.27, 3.29 to be considered as 3.25 and 3.20, 3.22, 3.24 to be considered as 3.20 ? any advice.

Mec-iS commented on Dec 18, 2018

Decimal has a quite high impact on performances:

In [12]: timeit.timeit("round(7.325, 2)")


Out[12]: 0.39438656000129413

In [9]: timeit.timeit("from decimal import Decimal, ROUND_DOWN;round(Decimal('7.325'), 2)")


Out[9]: 0.9643819720076863

In [21]: timeit.timeit("float('%.2f' % 7.325)")


Out[21]: 0.3827699579996988

rblack commented on Feb 19, 2019

@Mec-iS While Decimal is indeed slower, I believe your example is not totally correct. You measure imports
together with initializing Decimal object. IMO timeit.timeit(stmt="round(d, 2)", setup="from decimal
import Decimal, ROUND_DOWN;d=Decimal('7.325')") would be closer analog of rounding floats.

weaming commented on Oct 15, 2019

Slow but correct, I choose Decimal .


https://gist.github.com/jackiekazil/6201722 6/7
10/19/2020 How do I round to 2 decimals in python? · GitHub
,

HeyHugo commented on Nov 8, 2019 • edited

Money class method from py-moneyed:

def round(self, ndigits=0):


"""
Rounds the amount using the current ``Decimal`` rounding algorithm.
"""
if ndigits is None:
ndigits = 0
return self.__class__(
amount=self.amount.quantize(Decimal('1e' + str(-ndigits))),
currency=self.currency
)

orrinstimpson commented on Mar 12

python has a built in round function that allows you to specify the number of decimals to truncate too. See
https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points

https://gist.github.com/jackiekazil/6201722 7/7

You might also like