How can I round up a number to the second decimal place in python? For example:
0.022499999999999999
Should round up to 0.03
<
Note that the ceil(num * 100) / 100
trick will crash on some degenerate inputs, like 1e308. This may not come up often but I can tell you it just cost me a couple of days. To avoid this, "it would be nice if" ceil()
and floor()
took a decimal places argument, like round() does... Meanwhile, anyone know a clean alternative that won't crash on inputs like this? I had some hopes for the decimal package but it seems to die too:
>>> from math import ceil
>>> from decimal import Decimal, ROUND_DOWN, ROUND_UP
>>> num = 0.1111111111000
>>> ceil(num * 100) / 100
0.12
>>> float(Decimal(num).quantize(Decimal('.01'), rounding=ROUND_UP))
0.12
>>> num = 1e308
>>> ceil(num * 100) / 100
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
OverflowError: cannot convert float infinity to integer
>>> float(Decimal(num).quantize(Decimal('.01'), rounding=ROUND_UP))
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
Of course one might say that crashing is the only sane behavior on such inputs, but I would argue that it's not the rounding but the multiplication that's causing the problem (that's why, eg, 1e306 doesn't crash), and a cleaner implementation of the round-up-nth-place fn would avoid the multiplication hack.
The round funtion stated does not works for definate integers like :
a=8
round(a,3)
8.0
a=8.00
round(a,3)
8.0
a=8.000000000000000000000000
round(a,3)
8.0
but , works for :
r=400/3.0
r
133.33333333333334
round(r,3)
133.333
Morever the decimals like 2.675 are rounded as 2.67 not 2.68.
Better use the other method provided above.
Here's a simple way to do it that I don't see in the other answers.
To round up to the second decimal place:
>>> n = 0.022499999999999999
>>>
>>> -(-n//.01) * .01
0.03
>>>
Other value:
>>> n = 0.1111111111111000
>>>
>>> -(-n//.01) * .01
0.12
>>>
With floats there's the occasional value with some minute imprecision, which can be corrected for if you're displaying the values for instance:
>>> n = 10.1111111111111000
>>>
>>> -(-n//0.01) * 0.01
10.120000000000001
>>>
>>> f"{-(-n//0.01) * 0.01:.2f}"
'10.12'
>>>
A simple roundup function with a parameter to specify precision:
>>> roundup = lambda n, p: -(-n//10**-p) * 10**-p
>>>
>>> # Or if you want to ensure truncation using the f-string method:
>>> roundup = lambda n, p: float(f"{-(-n//10**-p) * 10**-p:.{p}f}")
>>>
>>> roundup(0.111111111, 2)
0.12
>>> roundup(0.111111111, 3)
0.112
The python round
function could be rounding the way not you expected.
You can be more specific about the rounding method by using Decimal.quantize
eg.
from decimal import Decimal, ROUND_HALF_UP
res = Decimal('0.25').quantize(Decimal('0.0'), rounding=ROUND_HALF_UP)
print(res)
# prints 0.3
More reference:
https://gist.github.com/jackiekazil/6201722
Extrapolating from Edwin's answer:
from math import ceil, floor
def float_round(num, places = 0, direction = floor):
return direction(num * (10**places)) / float(10**places)
To use:
>>> float_round(0.21111, 3, ceil) #round up
>>> 0.212
>>> float_round(0.21111, 3) #round down
>>> 0.211
>>> float_round(0.21111, 3, round) #round naturally
>>> 0.211