Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Money data type (http://www.velocityreviews.com/forums/t322617-money-data-type.html)

Batista, Facundo 09-17-2003 06:28 PM

Money data type
 
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


Lukasz Pankowski 09-17-2003 08:17 PM

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 =*=

Asun Friere 09-18-2003 01:38 AM

Re: Money data type
 
"Batista, Facundo" <FBatista@uniFON.com.ar> wrote in message news:<mailman.1063823424.16083.python-list@python.org>...
> 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 09-18-2003 06:22 AM

Re: Money data type
 
afriere@yahoo.co.uk (Asun Friere) wrote in message news:<38ec68a6.0309171738.3b3becda@posting.google. com>...
>
> def __repr__ (self) :
> return "$%.2f" % self



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


All times are GMT. The time now is 02:02 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.