aproximate a number

Thomas Bartkus thomasbartkus at comcast.net
Mon Aug 29 22:22:12 EDT 2005


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





More information about the Python-list mailing list