RE: Rounding up to the next 100

Discussion in 'Python' started by Michael.Coll-Barth@VerizonWireless.com, Jan 21, 2010.

  1. Guest


    > 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.
     
    , Jan 21, 2010
    #1
    1. Advertising

  2. noydb Guest

    On Jan 21, 4:30 pm, 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.
     
    noydb, Jan 21, 2010
    #2
    1. Advertising

  3. * :
    >
    >
    >> 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
     
    Alf P. Steinbach, Jan 21, 2010
    #3
  4. noydb Guest

    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.
     
    noydb, Jan 21, 2010
    #4
  5. * 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.
     
    Alf P. Steinbach, Jan 21, 2010
    #5
  6. casevh Guest

    On Jan 21, 1:58 pm, noydb <> 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
     
    casevh, Jan 22, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Deniz Bahar
    Replies:
    2
    Views:
    485
    Andrey Tarasevich
    Mar 9, 2005
  2. noydb

    Rounding up to the next 100

    noydb, Jan 21, 2010, in forum: Python
    Replies:
    4
    Views:
    721
  3. Dan Janowski
    Replies:
    73
    Views:
    687
    trans. (T. Onoma)
    Nov 2, 2004
  4. fred
    Replies:
    3
    Views:
    286
    Zifud
    Mar 17, 2005
  5. Replies:
    5
    Views:
    891
Loading...

Share This Page