float / rounding question

Necmettin Begiter necmettin.begiter at gmail.com
Mon Feb 25 07:05:11 EST 2008


25 February 2008 Monday 12:44:46 tarihinde helen.m.flynn at gmail.com şunları yazmıştı:
> Hi I'm very much a beginner with Python.
> I want to write a function to convert celcius to fahrenheit like this
> one:
> 
> def celciusToFahrenheit(tc):
>     tf = (9/5)*tc+32
>     return tf
> 
> I want the answer correct to one decimal place, so
> celciusToFahrenheit(12) would return 53.6.
> 
> Of course the function above returns 53.600000000000001.
> 
> How do I format it correctly?

Use the round(number,digits) function:

tf = round((9/5)*tc+32,1)



More information about the Python-list mailing list