Money data type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Batista, Facundo

    Money data type

    Can't find it.

    I mean something like:
    [color=blue][color=green]
    >> m1 = Money(decimal=2 )
    >> m2 = Money(decimal=2 )
    >> m1.value = 3.20
    >> m2.value = 2.15
    >> print m1 + m2[/color][/color]
    5.35

    5.35! Not 5.3500000000000 001 neither 5.3499999999999 9999999.

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

    Thanks!

    .. Facundo

  • Lukasz Pankowski

    #2
    Re: Money data type

    look at http://fixedpoint.sourceforge.net/ it gives you numeric object
    for monetary applications (I have never used it)

    --

    =*= Lukasz Pankowski =*=

    Comment

    • Asun Friere

      #3
      Re: Money data type

      "Batista, Facundo" <FBatista@uniFO N.com.ar> wrote in message news:<mailman.1 063823424.16083 .python-list@python.org >...[color=blue]
      > Can't find it.
      >
      > I mean something like:
      >[color=green][color=darkred]
      > >> m1 = Money(decimal=2 )
      > >> m2 = Money(decimal=2 )
      > >> m1.value = 3.20
      > >> m2.value = 2.15
      > >> print m1 + m2[/color][/color]
      > 5.35
      >
      > 5.35! Not 5.3500000000000 001 neither 5.3499999999999 9999999.
      >
      > I think this is not a rare thing, but I can't find it in the standar library
      > neither the package index.
      >
      > Thanks!
      >
      > . Facundo[/color]

      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 ...
      [color=blue][color=green][color=darkred]
      >>> a = Dollars(125.45)
      >>> a[/color][/color][/color]
      $125.45[color=blue][color=green][color=darkred]
      >>> a + 25.6667[/color][/color][/color]
      $151.12[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]


      Note this is _not_ to your spec (ie output includes a currency symbol
      and input is simply a float).

      Comment

      • Asun Friere

        #4
        Re: Money data type

        afriere@yahoo.c o.uk (Asun Friere) wrote in message news:<38ec68a6. 0309171738.3b3b ecda@posting.go ogle.com>...[color=blue]
        >
        > def __repr__ (self) :
        > return "$%.2f" % self[/color]


        Umm ...you might want to do the same to __str__ if you want to print ... :/

        Comment

        Working...