float / rounding question

John Machin sjmachin at lexicon.net
Mon Feb 25 17:14:30 EST 2008


On Feb 26, 7:14 am, "Terry Reedy" <tjre... at udel.edu> wrote:
> <helen.m.fl... at gmail.com> wrote in message
>
> news:d4d9e9d6-b0e0-4063-a5b2-456bcea5a6ce at z17g2000hsg.googlegroups.com...
> | 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
>
> Unless you are importing 'integer division' or using 3.0, that should be
> 9.0/5.0.

Has the syntax changed? I thought it was:
    from __future__ import division

The OP may wish to avoid the confusion and the pointless division by
using:
   tf = 1.8 * tc + 32


>
> | I want the answer correct to one decimal place, so
> | celciusToFahrenheit(12) would return 53.6.
>
> As written, running above on 2.x returns 44.
>
> tjr




More information about the Python-list mailing list