Jase
Jase

Reputation: 21

Python Single Decimal

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))

Upvotes: 0

Views: 70

Answers (3)

Nova
Nova

Reputation: 452

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.

Upvotes: 1

Chris
Chris

Reputation: 36451

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

Upvotes: 0

Aleksa Majkic
Aleksa Majkic

Reputation: 803

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))

Upvotes: 1

Related Questions