money class?

Mark McEahern mark at mceahern.com
Thu Feb 7 04:40:34 CET 2002


> From: Brian Quinlan [mailto:brian at sweetapp.com]
> Are you looking for any class that happens to be named "Money" or are
> you looking for some particular functionality?

Dang, and I thought I could get away with hoping someone on the list would
read my mind.  ;-)

Consider this:

  #! /usr/bin/env python
  # money.py
  cost = 5.99
  percentDiscount = 10
  months = 3
  subTotal = cost * 3
  discount = subTotal * (percentDiscount * 1.0) / 100
  total = subTotal - discount
  expectedTotal = 16.17
  print total == expectedTotal

I want something that will just do the right thing and return true here.
Now that I've gone through the trouble of demonstrating why I can't simply
rely on float, I realize I can turn this into a test case and I'll be
halfway to having my own class.

I think the Money class is basically going to end up overriding special
methods like __mul__ to make sure that percentage calculations return the
expected result.  Or, maybe it should look like this:

	cost = Money(5.99)
	discount = cost.percent(10)
	print isinstance(discount, Money)
	1

Sorry to continue to be vague.  If I end up writing a money class, I'll
share it with the list so that everyone can see how dumb I am.  ;-)

Cheers,

// mark





More information about the Python-list mailing list