Rounding up to the next 100

2010-01-21 Thread 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.

Thanks for any help!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rounding up to the next 100

2010-01-21 Thread Arnaud Delobelle
noydb jenn.du...@gmail.com writes:

 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.

 Thanks for any help!

 XstrNmbr = 3579.127893
 round(float(XstrNmbr), -2)
3600.0
 

HTH

-- 
Arnaud
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rounding up to the next 100

2010-01-21 Thread Arnaud Delobelle
Arnaud Delobelle arno...@googlemail.com writes:


  XstrNmbr = 3579.127893

I meant
 XstrNmbr = 3579.127893

  round(float(XstrNmbr), -2)
 3600.0

-- 
Arnaud
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Rounding up to the next 100

2010-01-21 Thread Michael . Coll-Barth
 

 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?


The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure.  If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof.  Thank you.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rounding up to the next 100

2010-01-21 Thread Florian Diesch
noydb jenn.du...@gmail.com writes:

 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.

math.ceil(3579.127893/100)*100


   Florian
-- 
GUIs programmieren mit Python und Glade:
http://www.florian-diesch.de/doc/python-und-glade/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rounding up to the next 100

2010-01-21 Thread Alf P. Steinbach

* michael.coll-ba...@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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Rounding up to the next 100

2010-01-21 Thread noydb
On Jan 21, 4:30 pm, michael.coll-ba...@verizonwireless.com wrote:
  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?

 The information contained in this message and any attachment may be
 proprietary, confidential, and privileged or subject to the work
 product doctrine and thus protected from disclosure.  If the reader
 of this message is not the intended recipient, or an employee or
 agent responsible for delivering this message to the intended
 recipient, you are hereby notified that any dissemination,
 distribution or copying of this communication is strictly prohibited.
 If you have received this communication in error, please notify me
 immediately by replying to this message and deleting it and all
 copies and backups thereof.  Thank you.

Thanks Arnaud!

Michael - Nothing is wrong with round -- when I tried it initially, I
was confused on the base -- seeing it from this example helped clear
it up.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rounding up to the next 100

2010-01-21 Thread noydb
Sorry, although what I really need is the string-number rounded UP
every time.  So if the number is 3890.32, it needs to go to 3900; if
the number is 3811.345, it needs to go to 3900 also.

So, Florian's answer works.

-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Rounding up to the next 100

2010-01-21 Thread Michael . Coll-Barth
 

 From: Arnaud Delobelle
 
 
   XstrNmbr = 3579.127893
 
 I meant
  XstrNmbr = 3579.127893
 
   round(float(XstrNmbr), -2)
  3600.0

Ah, then you will need to cast it first.

 XstrNmbr = '3579.127893'
 round(float(XstrNmbr) ,-2)
3600.0



The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure.  If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof.  Thank you.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rounding up to the next 100

2010-01-21 Thread Alf P. Steinbach

* noydb:

Sorry, although what I really need is the string-number rounded UP
every time.  So if the number is 3890.32, it needs to go to 3900; if
the number is 3811.345, it needs to go to 3900 also.

So, Florian's answer works.


You might also consider

  -100*(-3579.127893//100)

:-)

Which avoids the math.ceil but assumes the number is positive (or zero).


Cheers  hth.,

- Alf

PS: Note that this trick doesn't work with most other common languages that I'm 
familiar with, since the round towards zero instead of down to minus infinity, 
but Python has more clean semantics in this regard.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Rounding up to the next 100

2010-01-21 Thread casevh
On Jan 21, 1:58 pm, noydb jenn.du...@gmail.com wrote:
 Sorry, although what I really need is the string-number rounded UP
 every time.  So if the number is 3890.32, it needs to go to 3900; if
 the number is 3811.345, it needs to go to 3900 also.

 So, Florian's answer works.

Another option is using math.ceil and math.floor.

 import math
 100*math.ceil(1234.5678/100)
1300
 100*math.floor(1234.5678/100)
1200
 100*math.ceil(-1234.5678/100)
-1200
 100*math.floor(-1234.5678/100)
-1300

casevh
-- 
http://mail.python.org/mailman/listinfo/python-list