Rounding up to the next 100

Alf P. Steinbach alfps at start.no
Thu Jan 21 16:53:27 EST 2010


* Michael.Coll-Barth at VerizonWireless.com:
>  
> 
>> From: noydb
> 
>> If one has a floating number as a string, is there a spiffy way to
>> round that string-number UP to the nearest 100?
>>
>> XstrNmbr = 3579.127893 -- would want to round that to 3600.
> 
> 
> What's wrong with round?  round( XstrNmbr, -2 ) seems to do the trick.
> Or do you want to get rid of the decimal point as well?

Perhaps completely irrelevant, but just in passing, round() changed semantics 
from 2.x to 3.x, in 3.x always returning int when called with just 1 argument:


   >>> import sys
   >>> sys.version
   '2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)]'
   >>> print round.__doc__
   round(number[, ndigits]) -> floating point number

   Round a number to a given precision in decimal digits (default 0 digits).
   This always returns a floating point number.  Precision may be negative.
   >>> _


   >>> import sys
   >>> sys.version
   '3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)]'
   >>> print( round.__doc__ )
   round(number[, ndigits]) -> number

   Round a number to a given precision in decimal digits (default 0 digits).
   This returns an int when called with one argument, otherwise the
   same type as the number. ndigits may be negative.
   >>> _


Might be useful to know regarding "get rid of the decimal point": in 3.x 
round(x) does that, in 2.x it doesn't.


Cheers,

- Alf



More information about the Python-list mailing list