a = 310.97
b = 233.33
sum= 0.0
for i in [a,b]:
sum += i
print(sum)
py2 o/p: 544.3
py3 o/p: 544.3000000000001
Any way to report py3 output as same as py2 with futurizing? without using round-off ?
a = 310.97
b = 233.33
sum= 0.0
for i in [a,b]:
sum += i
print(sum)
py2 o/p: 544.3
py3 o/p: 544.3000000000001
Any way to report py3 output as same as py2 with futurizing? without using round-off ?
You could convert the values to integers before performing the operation and afterwards divide by a constant e.g. 100.0 in this case.
a = 310.97
b = 233.33
c = int(a * 100)
d = int(b * 100)
sum = 0
for i in [c,d]:
sum += i
result = sum / 100.0
print(result) # 544.3
The reason for the difference is the precision in the conversion from float to string in the two versions of Python.
a = 310.97
b = 233.33
sum = 0.0
for i in [a,b]:
sum += i
print("{:.12g}".format(x)) # 544.3
See this answer for further details: Python format default rounding when formatting float number