[Tutor] Rounding up/down integer numbers!

Gregor Lingl glingl@aon.at
Fri Aug 1 05:01:05 2003


Wilhelmsen Jan schrieb:

> Hi!
>
>  
>
> I have the need to round up some numbers to the nearest 100. (01-49 
> down and 50-99 round up)
>
>  
>
> How do you do this most efficiently?
>
>  
>
Python has a function round, which returns numbers of type double:

 >>> print round(1123.456789, 4)
1123.4568
 >>> print round(1123.456789, 2)
1123.46
 >>> print round(1123.456789, 0)
1123.0

This function has a wonderful magic property:

 >>> print round(1123.456789, -1)
1120.0
 >>> print round(1123.456789, -2)
1100.0

If you need an integer as a result use int to convert type:

 >>> print int(round(1123.456789, -2))
1100
 >>> print int(round(8359980, -2))
8360000

HTH, Gregor





> Ex:
>
>  
>
> 8356440
>
> Should be rounded down to:
>
> 8356400
>
> and:
>
> 8356499
>
> should be rounded up to:
>
> 8356500
>
>  
>
> So far I managed but what if:
>
>  
>
> 8359980
>
> Then the number should be rounded up to:
>
> 8360000
>
>  
>
> Can anyone give me a clue to this?
>
>  
>
> I think maybe I have to set up a range with split 100 and use a while 
> syntax
>
>  
>
> But I'm pretty new to programming so I'm not sure how to do this
>
>  
>
>  
>
> Thanks
>
>  
>
> Regards
>
>  
>
> Jan Wilhelmsen
>
> IT-Technician
>
> Bilia Personbil as
>
>  
>
>
>
> -------------------------------------------------------------------------------
> This verifies that this e-mail has been scanned for virus and deemed 
> virus-free
> according to F-secure Content Scanner 5.0
> Fri, 1 Aug 2003 10:23:39 +0200 GMT
> -------------------------------------------------------------------------------