Python Single Decimal

Question:

When I enter this code the answer ends with 2 characters behind the decimal. How do I make this only have 1 number behind it?

tempature=float(input("Enter the temp(F):"))
formant_tempature = f"{tempature:2f}"
print(round(((int(tempature)-32)*5/9)+273.15,2))
Asked By: Jase

||

Answers:

When you used round function you have specified that you want two decimal places. Just replace 2 with a number 1.

print(round(((int(tempature)-32)*5/9)+273.15,1))
Answered By: Aleksa Majkic

I’m not sure why you’d do any math just to present this rounded, when you can simply use an f-string to specify outputting the temperature with a single decimal place precision.

>>> temperature = 43.8934
>>> print(f"Temperature is {temperature:.1f} degrees")
Temperature is 43.9 degrees
>>> print(f"Temperature is {temperature * 1.8 + 32:.1f} degrees farenheit")
Temperature is 111.0 degrees farenheit
Answered By: Chris

You are using the string formatting operator for that ( formant_tempature = f"{tempature:2f}" )

What about formant_tempature = f"{tempature:1f}"

Like if you want it to display 5 decimals, just change it to f"{tempature:5f}"

And so on.

And for the round method, change 2 to 1.

Answered By: Nova
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.