I have some issues with floating point numbers and rounding them.
I have a function that takes a dictionary as an input. It finds the average of all the values and then calculates the difference between each value and the average. The answers should be rounded to two decimal places.
I can't figure out why it's not returning floating point values that are properly rounded. The funny thing is that when I print out x[i]
inside the for loop, it looks exactly how I want it. Also, I want to keep the values a number and not a formatted string.
def avgstuff(x):
ppv = sum(x.values())/float(len(x))
for i in x:
x[i] = round(x[i] - ppv,2)
return x
print avgstuff({'A': 40, 'C': 10, 'B': 25, 'E': 58, 'D': 153})
current output:
{'A': -17.199999999999999, 'C': -47.200000000000003, 'B': -32.200000000000003, 'E': 0.80000000000000004, 'D': 95.799999999999997}
Output I want:
{'A': -17.20, 'C': -47.20, 'B': -32.20, 'E': 0.80, 'D': 95.80}
Output I don't want:
{'A': '-17.20', 'C': '-47.20', 'B': '-32.20', 'E': '0.80', 'D': '95.80'}