aproximate a number

Grant Edwards grante at visi.com
Mon Aug 29 22:44:44 EDT 2005


On 2005-08-30, Devan L <devlai at gmail.com> 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.

That's what the OP said he wanted.  The next bigger integer
after 2.0 is 3.

-- 
Grant Edwards                   grante             Yow!  I'd like TRAINED
                                  at               SEALS and a CONVERTIBLE on
                               visi.com            my doorstep by NOON!!



More information about the Python-list mailing list