95

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
hsinxh
  • 1,865
  • 6
  • 21
  • 25
  • 4
    round(8.8333333333333339 , 2) will give 8.83 never 8.84 it seems. – VGE Dec 23 '10 at 12:16
  • 45
    Why 8.84? 8.8333333... should be rounded to 8.83 when aiming for two decimal places. – Tim Pietzcker Dec 23 '10 at 12:17
  • duplicate: http://stackoverflow.com/questions/56820/round-in-python-doesnt-seem-to-be-rounding-properly – mouad Dec 23 '10 at 12:18
  • 1
    if you want to print the value use a format such as print "%.2f"%8.8333333333333339. this will print the value with 2 digit – VGE Dec 23 '10 at 12:19
  • I edited the title because this comes up as an early result for "python round float," and it's probably not what most people are looking for. Hopefully, the new title makes things more clear. – jpmc26 Oct 04 '17 at 04:01
  • 1
    Does this answer your question? [Limiting floats to two decimal points](https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points) – Irfan wani Jan 19 '21 at 06:45

12 Answers12

106

8.833333333339 (or 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 ceil:

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:

screenshot of loan calculator output

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 112 of the total amount is being paid. That made the final payment a little smaller than usual — leaving a remaining unpaid balance of only 8.76.

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
70

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 decimal module:

>>> import decimal
>>> a = decimal.Decimal("8.833333333339")
>>> print(round(a,2))
8.83
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
23

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')
>>> 
casevh
  • 11,093
  • 1
  • 24
  • 35
14

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

3.9800000000000004

But if you enclose it in a round() function like so

print(round(total_price,2))

The output equals

3.98

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.

user2252471
  • 289
  • 3
  • 9
9

The easiest way to do this is by using the below function, which is built in:

format()

For example:

format(1.242563,".2f")

The output would be:

1.24

Similarly:

format(9.165654,".1f")

would give:

9.2
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • The format answer was given in 2010 [here](https://stackoverflow.com/a/4518767/8928024). You could have updated that answer instead. – ZF007 Feb 27 '18 at 21:08
7

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?)
Andreas Løve Selvik
  • 1,262
  • 16
  • 25
6

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)
Rosh Oxymoron
  • 20,355
  • 6
  • 41
  • 43
5

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

twohot
  • 51
  • 1
  • 2
2

Here is my solution for the round up/down problem

< .5  round down

> = .5  round up

import math

def _should_round_down(val: float):
    if val < 0:
        return ((val * -1) % 1) < 0.5
    return (val % 1) < 0.5

def _round(val: float, ndigits=0):
    if ndigits > 0:
        val *= 10 ** (ndigits - 1)
    is_positive = val > 0
    tmp_val = val
    if not is_positive:
        tmp_val *= -1
    rounded_value = math.floor(tmp_val) if _should_round_down(val) else math.ceil(tmp_val)
    if not is_positive:
        rounded_value *= -1
    if ndigits > 0:
        rounded_value /= 10 ** (ndigits - 1)
    return rounded_value

# test
# nr = 12.2548
# for digit in range(0, 4):
#     print("{} decimals : {} -> {}".format(digit, nr, _round(nr, digit)))

# output
# 0 decimals : 12.2548 -> 12
# 1 decimals : 12.2548 -> 12.0
# 2 decimals : 12.2548 -> 12.3
# 3 decimals : 12.2548 -> 12.25
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
opra
  • 47
  • 3
2

Use the decimal module: http://docs.python.org/library/decimal.html

ََََََ

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
orip
  • 73,323
  • 21
  • 116
  • 148
  • >>> decimal.getcontext().prec = 3 >>> decimal.Decimal(106)/decimal.Decimal(12) Decimal('8.83') this is what I got, and not 8.84 – hsinxh Dec 23 '10 at 12:35
1

I have this code:

tax = (tax / 100) * price

and then this code:

tax = round((tax / 100) * price, 2)

round worked for me

Nic56
  • 11
  • 4
  • You solved my problem. I could only round with this. Nothing else worked. ```renewal_percentage=round(df_x_renewal_count/df_all_count*100)``` – Kierk Sep 05 '20 at 19:36
0

Here is a simple function to do this for you:

def precision(num,x):
    return "{0:.xf}".format(round(num))

Here, num is the decimal number. x is the decimal up to where you want to round a floating number.

The advantage over other implementation is that it can fill zeros at the right end of the decimal to make a deciaml number up to x decimal places.

Example 1:

precision(10.2, 9)

will return

10.200000000 (up to 9 decimal points)

Example 2:

precision(10.2231, 2)

will return

10.22 (up to two decimal points)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Akash Kandpal
  • 3,126
  • 28
  • 25