float / rounding question
casevh
casevh at gmail.com
Mon Feb 25 08:26:03 EST 2008
On Feb 25, 2: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?
That is the normal behavior for binary (radix-2) numbers. Just like it
is impossible write 1/3 exactly as a decimal (radix-10) number, 536/10
cannot be written exactly as a binary number. If you really need
decimal numbers, use the Decimal class.
See http://docs.python.org/tut/node16.html.
casevh
More information about the Python-list
mailing list