How Do I Round To 2 Decimals in Python - GitHub
How Do I Round To 2 Decimals in Python - GitHub
· GitHub
jackiekazil / rounding_decimals.md
Last active 25 days ago
Star
rounding_decimals.md
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
Output: 2.29
our_value = Decimal(16.0/7)
output = Decimal(our_value.quantize(Decimal('.01'), rounding=ROUND_HALF_UP))
print output
Output: 2.29
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
Or am I wrong?
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.
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:
Which I don't think is what you want? You can of course set prec to 4, but that's not really a solution.
if i'll write
v= Decimal(x)
x is a float() type ,then can i use round(v,2)?
Decimal('1.4')
https://gist.github.com/jackiekazil/6201722 4/7
10/19/2020 How do I round to 2 decimals in python? · GitHub
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
nice
Ref: https://stackoverflow.com/questions/3410976/how-to-round-a-number-to-significant-figures-in-python
for Python 3
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
print ("%.2f" % a)
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 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.
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