aproximate a number

Devan L devlai at gmail.com
Mon Aug 29 22:31:17 EDT 2005


Thomas Bartkus wrote:
> On Sun, 28 Aug 2005 23:11:09 +0200, billiejoex wrote:
>
> > Hi all. I'd need to aproximate a given float number into the next (int)
> > bigger one. Because of my bad english I try to explain it with some example:
> >
> > 5.7 --> 6
> > 52.987 --> 53
> > 3.34 --> 4
> > 2.1 --> 3
> >
>
> The standard way to do this is thus:
>
> def RoundToInt(x):
>     """ Round the float x to the nearest integer """
>     return int(round(x+0.5))
>
> x = 5.7
> print x, '-->', RoundToInt(x)
> x = 52.987
> print x, '-->', RoundToInt(x)
> x = 3.34
> print x, '-->', RoundToInt(x)
> x = 2.1
> print x, '-->', RoundToInt(x)
>
> 5.7 --> 6
> 52.987 --> 53
> 3.34 --> 4
> 2.1 --> 3

RoundToInt(2.0) will give you 3.




More information about the Python-list mailing list