I have two functions:
def centify(amount_nbr):
if amount_nbr:
try:
amount_nbr = int(float(amount_nbr) * 100)
except ValueError:
amount_nbr = 0
else:
amount_nbr = 0
return amount_nbr
def uncentify(amount_nbr):
if amount_nbr:
amount_nbr /= 100.
else:
amount_nbr = 0
return amount_nbr
When I test a load of floats:
for i in range(0, 100):
i *= 0.01
if i != uncentify(centify(i)):
print "%f - %f" % (uncentify(centify(i)), i)
It returns that the rounding is wrong for:
0.280000 - 0.290000
0.350000 - 0.350000
0.410000 - 0.410000
0.470000 - 0.470000
0.570000 - 0.570000
0.570000 - 0.580000
0.690000 - 0.690000
0.700000 - 0.700000
0.820000 - 0.820000
0.830000 - 0.830000
0.940000 - 0.940000
0.950000 - 0.950000
What is going on? And how do I make them the exact inverse?
I need to fix the fact that:
num = 316805.79
print centify(num)
returns 31680578
rather than 31680579