-1

How to get correct number after calculation? For instance, I want 2.2 - 1 equal to 1.2 and not 1.2000000000002. Wanna get exactly correct answer without rounding the number itself and without just simply changing the way of printing the number via print('%1.1f' % number).

bL1deR
  • 1
  • 1

2 Answers2

1

Since you don't want string formatting, You can do round:

>>> a=2.2
>>> b=1
>>> round(a-b,1)
1.2
>>> 
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

Use format,

In [7]: '{:.2f}'.format(2.2 - 1)
Out[7]: '1.20'
Rahul K P
  • 15,740
  • 4
  • 35
  • 52