Suppose I have
8.8333333333333339, and I want to convert it to
8.84. How can I accomplish this in Python?
round(8.8333333333333339, 2) gives
8.83 and not
8.84. I am new to Python or programming in general.
I don’t want to print it as a string, and the result will be further used. For more information on the problem, please check Tim Wilson’s Python Programming Tips: Loan and payment calculator.
8.833333333333334, the result of
106.00/12) properly rounded to two decimal places is
8.83. Mathematically it sounds like what you want is a ceiling function. The one in Python’s
math module is named
import math v = 8.8333333333333339 print(math.ceil(v*100)/100) # -> 8.84
Respectively, the floor and ceiling functions generally map a real number to the largest previous or smallest following integer which has zero decimal places — so to use them for 2 decimal places the number is first multiplied by 102 (or 100) to shift the decimal point and is then divided by it afterwards to compensate.
If you don’t want to use the
math module for some reason, you can use this (minimally tested) implementation I just wrote:
def ceiling(x): n = int(x) return n if n-1 < x <= n else n+1
How all this relates to the linked Loan and payment calculator problem:
From the sample output it appears that they rounded up the monthly payment, which is what many call the effect of the ceiling function. This means that each month a little more than 1?12 of the total amount is being paid. That made the final payment a little smaller than usual — leaving a remaining unpaid balance of only
It would have been equally valid to use normal rounding producing a monthly payment of
8.83 and a slightly higher final payment of
8.87. However, in the real world people generally don’t like to have their payments go up, so rounding up each payment is the common practice — it also returns the money to the lender more quickly.
This is normal (and has nothing to do with Python) because 8.83 cannot be represented exactly as a binary float, just as 1/3 cannot be represented exactly in decimal (0.333333… ad infinitum).
If you want to ensure absolute precision, you need the
import decimal a = decimal.Decimal("8.833333333339") print(round(a,2)) 8.83
You want to use the decimal module but you also need to specify the rounding mode. Here’s an example:
import decimal decimal.Decimal('8.333333').quantize(decimal.Decimal('.01'), rounding=decimal.ROUND_UP) Decimal('8.34') decimal.Decimal('8.333333').quantize(decimal.Decimal('.01'), rounding=decimal.ROUND_DOWN) Decimal('8.33')
A much simpler way is to simply use the round() function. Here is an example.
total_price = float() price_1 = 2.99 price_2 = 0.99 total_price = price_1 + price_2
If you were to print out total_price right now you would get
But if you enclose it in a round() function like so
The output equals
The round() function works by accepting two parameters. The first is the number you want to round. The second is the number of decimal places to round to.
If you round 8.8333333333339 to 2 decimals, the correct answer is 8.83, not 8.84. The reason you got 8.83000000001 is because 8.83 is a number that cannot be correctly reprecented in binary, and it gives you the closest one. If you want to print it without all the zeros, do as VGE says:
print "%.2f" % 8.833333333339 #(Replace number with the variable?)
The easiest way to do this is by using the below function, which is built in:
The output would be:
If you want to round, 8.84 is the incorrect answer. 8.833333333333 rounded is 8.83 not 8.84. If you want to always round up, then you can use math.ceil. Do both in a combination with string formatting, because rounding a float number itself doesn’t make sense.
"%.2f" % (math.ceil(x * 100) / 100)
Just for the record. You could do it this way:
def roundno(no): return int(no//1 + ((no%1)/0.5)//1)
There, no need for includes/imports