Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: Rounding up to the next 100

Reply
Thread Tools

RE: Rounding up to the next 100

 
 
Michael.Coll-Barth@VerizonWireless.com
Guest
Posts: n/a
 
      01-21-2010


> 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.


 
Reply With Quote
 
 
 
 
noydb
Guest
Posts: n/a
 
      01-21-2010
On Jan 21, 4:30*pm, (E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-21-2010
* http://www.velocityreviews.com/forums/(E-Mail Removed):
>
>
>> 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
 
Reply With Quote
 
noydb
Guest
Posts: n/a
 
      01-21-2010
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.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-21-2010
* 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.
 
Reply With Quote
 
casevh
Guest
Posts: n/a
 
      01-22-2010
On Jan 21, 1:58*pm, noydb <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tetration (print 100^100^100^100^100^100^100^100^100^100^100^100^100^100) jononanon@googlemail.com C Programming 5 04-25-2012 08:49 PM
Rounding up to the next 100 noydb Python 4 01-21-2010 10:03 PM
CurrentElement->next = CurrentElement->next->next (UNDEFINED?) Deniz Bahar C Programming 2 03-09-2005 12:45 AM



Advertisments