Money data type

Discussion in 'Python' started by Batista, Facundo, Sep 17, 2003.

  1. Can't find it.

    I mean something like:

    >> m1 = Money(decimal=2)
    >> m2 = Money(decimal=2)
    >> m1.value = 3.20
    >> m2.value = 2.15
    >> print m1 + m2

    5.35

    5.35! Not 5.3500000000000001 neither 5.34999999999999999999.

    I think this is not a rare thing, but I can't find it in the standar library
    neither the package index.

    Thanks!

    .. Facundo
     
    Batista, Facundo, Sep 17, 2003
    #1
    1. Advertising

  2. Lukasz Pankowski, Sep 17, 2003
    #2
    1. Advertising

  3. Batista, Facundo

    Asun Friere Guest

    "Batista, Facundo" <> wrote in message news:<>...
    > Can't find it.
    >
    > I mean something like:
    >
    > >> m1 = Money(decimal=2)
    > >> m2 = Money(decimal=2)
    > >> m1.value = 3.20
    > >> m2.value = 2.15
    > >> print m1 + m2

    > 5.35
    >
    > 5.35! Not 5.3500000000000001 neither 5.34999999999999999999.
    >
    > I think this is not a rare thing, but I can't find it in the standar library
    > neither the package index.
    >
    > Thanks!
    >
    > . Facundo


    If it's merely a matter of representing itself you could do something
    blindingly simple like:

    class Dollars (float) :

    def __repr__ (self) :
    return "$%.2f" % self

    def __add__ (self, other) :
    return Dollars(float.__add__(self, other))

    #and so on for the other mathematical operators ...

    >>> a = Dollars(125.45)
    >>> a

    $125.45
    >>> a + 25.6667

    $151.12
    >>>



    Note this is _not_ to your spec (ie output includes a currency symbol
    and input is simply a float).
     
    Asun Friere, Sep 18, 2003
    #3
  4. Batista, Facundo

    Asun Friere Guest

    (Asun Friere) wrote in message news:<>...
    >
    > def __repr__ (self) :
    > return "$%.2f" % self



    Umm ...you might want to do the same to __str__ if you want to print ... :/
     
    Asun Friere, Sep 18, 2003
    #4
    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. Jensen bredal
    Replies:
    3
    Views:
    7,543
    M. Posseth
    Jun 16, 2004
  2. Sam
    Replies:
    1
    Views:
    8,878
    Prnda76
    Feb 11, 2010
  3. bb
    Replies:
    13
    Views:
    795
  4. Sam
    Replies:
    0
    Views:
    204
  5. Replies:
    1
    Views:
    114
    Bob Barrows [MVP]
    Mar 26, 2008
Loading...

Share This Page