float / rounding question

Jorge Godoy jgodoy at gmail.com
Mon Feb 25 12:04:47 CET 2008


sabatier wrote:

> On Feb 25, 10:44 am, helen.m.fl... at gmail.com wrote:
>> 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?
> 
> By the way, I tried this:
> 
> return '%2.1f' % tf but that returns a string instead of a number.
> 
> Any other suggestions?

But you are asking for a string on your format string above.

And also formatting make no sense in other context since a number is a
number and 53.600000 and 53.6 are the same number (besides precision). 

You are concerned with how numbers are represented in binary.  When
displaying the value use the format string you shown above and all will
work. 




More information about the Python-list mailing list