round() to nearest .05 ?

Discussion in 'Python' started by tertius, Nov 15, 2004.

  1. tertius

    tertius Guest

    Hi,

    I'm trying to round my float total to the nearest .05 cents.

    12.01 should produce 12.00
    0.14 should produce 0.10
    2.28 " 2.25
    703.81 " 703.80

    "%.02f"%100.0099 produces 100.01 (which I know is right)
    No combination of round and/or "%.02f" works for me.

    What's the best way to get there? Should I write a function to manage my
    rounding or is there a simpler/better way?

    TIA
    T
     
    tertius, Nov 15, 2004
    #1
    1. Advertising

  2. tertius

    Kent Johnson Guest

    tertius wrote:
    > Hi,
    >
    > I'm trying to round my float total to the nearest .05 cents.


    If you want the *nearest* .05, this works:
    >>> def fivecents(f):

    .... return round(f*2.0, 1) / 2.0
    ....
    >>> for f in [ 12.01, 0.14, 2.28, 703.81]:

    .... print f, fivecents(f)
    ....
    12.01 12.0
    0.14 0.15
    2.28 2.3
    703.81 703.8

    > 12.01 should produce 12.00
    > 0.14 should produce 0.10
    > 2.28 " 2.25
    > 703.81 " 703.80


    Your example seems to be rounding down always, not what I would call
    nearest. Here is one that always rounds toward zero:
    >>> def lowerfivecents(f):

    .... return int(f*20) / 20.0
    ....
    >>> for f in [ 12.01, 0.14, 2.28, 703.81]:

    .... print f, lowerfivecents(f)
    ....
    12.01 12.0
    0.14 0.1
    2.28 2.25
    703.81 703.8

    If you want to always round toward more negative, use math.floor()
    instead of int().

    Kent
    >
    > "%.02f"%100.0099 produces 100.01 (which I know is right)
    > No combination of round and/or "%.02f" works for me.
    >
    > What's the best way to get there? Should I write a function to manage my
    > rounding or is there a simpler/better way?
    >
    > TIA
    > T
     
    Kent Johnson, Nov 15, 2004
    #2
    1. Advertising

  3. On Mon, 15 Nov 2004 22:52:42 +0200, tertius <> wrote:

    >Hi,
    >
    >I'm trying to round my float total to the nearest .05 cents.

    Nearest which way? IWT 2.28 is nearer 2.30 than 2.25 ;-)
    ISTM you are rounding _down_ to the nearest nickel. So you want to
    drop fractional nickels. Which int will do if you calculate the number
    of nickels. Then you can divide by 20. to get dollars again
    >
    >12.01 should produce 12.00
    > 0.14 should produce 0.10
    > 2.28 " 2.25
    >703.81 " 703.80
    >
    >"%.02f"%100.0099 produces 100.01 (which I know is right)
    >No combination of round and/or "%.02f" works for me.
    >
    >What's the best way to get there? Should I write a function to manage my
    >rounding or is there a simpler/better way?


    One way, just to play with subclassing float for constrained initial value:

    >>> values = [12.01, 0.14, 2.28, 703.81]
    >>> class Nickels(float):

    ... def __new__(cls, val):
    ... return float.__new__(cls, int(val*20.0)/20.)
    ... def __repr__(self): return '<Nickels %.2f>'%float(self)
    ...
    >>> [Nickels(v) for v in values]

    [<Nickels 12.00>, <Nickels 0.10>, <Nickels 2.25>, <Nickels 703.80>]
    >>> for value in values: print '%8.2f => %8.2f'%(value,Nickels(value))

    ...
    12.01 => 12.00
    0.14 => 0.10
    2.28 => 2.25
    703.81 => 703.80

    but it's probably silly compared to

    >>> def roundnickel(dollars): return int(dollars*20.0)/20.0

    ...
    >>> for value in values: print '%8.2f => %8.2f'%(value, roundnickel(value))

    ...
    12.01 => 12.00
    0.14 => 0.10
    2.28 => 2.25
    703.81 => 703.80

    What do you want to do for negative numbers? int "rounds" towards zero.

    >>> for value in -.01,.01,-.09,.09: print '%8.2f => %8.2f'%(value, roundnickel(value))

    ...
    -0.01 => 0.00
    0.01 => 0.00
    -0.09 => -0.05
    0.09 => 0.05

    BTW, if you wanted to use the class with all float operators, you'd have
    to define the methods one way or another (there's some sneaky ways, but I think
    the class is just a side trip ;-)

    >>> Nickels(.09)

    <Nickels 0.05>
    >>> Nickels(.09) + .01

    0.060000000000000005

    Being a subclass of float, python figured out how to coerce Nickels
    to float for the addition. If we add an adding method, we get

    >>> Nickels.__add__ = lambda self,other: Nickels(float(self)+ float(other))
    >>> Nickels(.09) + .01

    <Nickels 0.05>
    >>> Nickels(.09) + .06

    <Nickels 0.10>

    too much work ;-)

    Regards,
    Bengt Richter
     
    Bengt Richter, Nov 15, 2004
    #3
  4. > What's the best way to get there? Should I write a function to manage my
    > rounding or is there a simpler/better way?


    Others have pointed out how to achieve the rounding - I just want to add
    that using floats for currency values isn't the best choice. Depending on
    your version of python, check out these links:

    http://fixedpoint.sourceforge.net/
    http://www.python.org/peps/pep-0327.html


    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, Nov 16, 2004
    #4
  5. tertius

    tertius Guest

    tertius wrote:
    > Hi,
    >
    > I'm trying to round my float total to the nearest .05 cents.
    >
    > 12.01 should produce 12.00
    > 0.14 should produce 0.10
    > 2.28 " 2.25
    > 703.81 " 703.80
    >
    > "%.02f"%100.0099 produces 100.01 (which I know is right)
    > No combination of round and/or "%.02f" works for me.
    >
    > What's the best way to get there? Should I write a function to manage my
    > rounding or is there a simpler/better way?
    >
    > TIA
    > T


    Thanks for all the help!
    I meant nearest towards zero.

    chrs
    T
     
    tertius, Nov 16, 2004
    #5
    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. Steve
    Replies:
    5
    Views:
    42,780
    Steve
    May 17, 2004
  2. Fred
    Replies:
    3
    Views:
    63,753
  3. Don
    Replies:
    30
    Views:
    6,337
    murari garg
    Mar 4, 2007
  4. round to the nearest power of 4

    , Jan 29, 2007, in forum: C Programming
    Replies:
    4
    Views:
    484
    David T. Ashley
    Jan 29, 2007
  5. Replies:
    4
    Views:
    511
Loading...

Share This Page