0

How do I instruct python that it should consider only 2 decimal spaces?

x = -1.7999999999999998 +  0.20000000000000018 +  0.20000000000000018  - 0.7999999999999998 + 2.2
print('%.02f' % x)

0.00

I have manually adjusted values as below. But is there any way to automate this?

y = -1.79 + 0.20 + 0.20 - 0.79 + 2.2
print('%.02f' % y)

Update: I am trying to calculate sum of mean differences. I am not getting expected values.

y = [1,3,3,2,5]

avg = sum(y) / len(y)

mylist = list()
for i in y:
    mylist.append(i - avg)

sum(mylist)
shantanuo
  • 31,689
  • 78
  • 245
  • 403

2 Answers2

0

You can do this :

"{0:.2f}".format(x)  

Or

round(x, 2)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
0

I don't know all of the context, but I wouldn't opt for anything fancy:

x = -1.7999999999999998 +  0.20000000000000018 +  0.20000000000000018  - 0.7999999999999998 + 2.2
x = round(x, 2)
print(x)

This should work fine.

Blonded
  • 137
  • 1
  • 11