[Tutor] Rounding a Python float to the nearest half integer

Sithembewena Lloyd Dube zebra05 at gmail.com
Fri Oct 8 15:39:35 CEST 2010


Thanks everyone,

I need to round to the nearest half (finally occured).

Made some chnages to Wayne's code as follows:

x = 4.4348
if x % 1 >= 0.5:
   round(x) # gives 5.0 if  the the value of the expression x % 1 exceeds
0.5
else:
   x = round(x) + 0.5 # gives 4.5, as in this case.

Many thanks!



On Fri, Oct 8, 2010 at 3:00 PM, Evert Rol <evert.rol at gmail.com> wrote:

> > I realise that one cannot have a half integer :) I meant how would one
> round off to the first decimal nearest to either 0.5, or a whole number.
> >
> > Ugh...does anyone get what I'm trying to articulate? :)
>
> Multiply by 2, round(), divide by 2?
>
>
> >
> > On Fri, Oct 8, 2010 at 2:51 PM, Sithembewena Lloyd Dube <
> zebra05 at gmail.com> wrote:
> > Hi folks,
> >
> > Supposing I had the float 4.4348 and I wished to round it off to the
> nearest half-integer upwards or downwards, how would I go about it?
> >
> > Many thanks...
> >
> > --
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101008/a451127f/attachment.html>


More information about the Tutor mailing list