0

was creating a celsius to Fahrenheit calculator and my code is simply:

c = float(input())
f = (c*(9/5))+32
print(f)

However, when I test this code with 52.0 as an input, the output is 126.000000001 instead of just 126.0. Why is this so and how do I fix it?

Thanks.

  • 1
    related: https://stackoverflow.com/questions/588004/is-floating-point-math-broken you could just print: `print(f"{f:.2f}")` (see [python string formatting](http://zetcode.com/python/fstring/)). – hiro protagonist Nov 03 '20 at 09:08
  • Or you could use the ```round()``` function, per my answer bellow – Oliver Hnat Nov 03 '20 at 09:12

1 Answers1

0

You could use the round() function

c = float(input())
f = (c*(9/5))+32
f = round(f, 2)
print(f)
Oliver Hnat
  • 797
  • 4
  • 19