-1

Problem with subtracting float numbers.

a=0.45
b=0.15
c=a-b
print (c,type(c))
c=int(100*(a-b))/100
print (c,type(c))

results: 0.30000000000000004 <class 'float'> 0.3 <class 'float'>

Is there a way to get only one decimal digit after period when using 'print' for output without using int function?

1 Answers1

0

Use a formatting string to specify exactly how you want the float value to be rounded:

>>> print(c)
0.30000000000000004
>>> print(f"{c:.1f}")
0.3
Samwise
  • 68,105
  • 3
  • 30
  • 44