[Pymoney-general] Currency's distrib() method
Brought to you by:
facundobatista
From: Facundo B. <fac...@gm...> - 2005-07-01 08:52:25
|
We've been thinking yesterday how the Currency's distrib method should work. We figured out three posibilities, but we're not be able to choose a winner. The basic idea is to distribute a Currency amount between different parts, and being sure that you're not losing information (well, money, ;). But we saw three methods of doing it: 1. Returning the different currencies, splitting almost equally:: >>> Currency(1).distrib(3) [Currency("0.33"), Currency("0.33"), Currency("0.34")] >>> Currency(1).distrib(7) [Currency("0.14"), Currency("0.14"), Currency("0.14"), Currency("0.14"), Currency("0.15"), Currency("0.15"), Currency("0.14")] The order of the results is not assured. The difference between max(results) and min(results) will be only one Less significant Digit. 2. Returning the almost exact part, with the rest (a kind of divmod() but not to integers):: >>> Currency(1).distrib(3) (Currency("0.33"), Currency("0.01")) >>> Currency(1).distrib(7) (Currency("0.14"), Currency("0.02")) If with the original value OV splited in N parts, it returns the division and the rest (DV, RS), the following applies: DV * N + RS =3D OV 3. Returning the different currencies, splitting differently:: >>> Currency(1).distrib([1,2]) [Currency("0.33"), Currency("0.67")] >>> Currency(3).distrib([2,1,3,3]) [Currency("0.66"), Currency("0.33"), Currency("1.01"), Currency("1.00")= ] The rest of the better-possible-division will be assigned to the greatest(s) value(s) of the result. What do you think? . Facundo Blog: http://www.taniquetil.com.ar/plog/ PyAr: http://www.python.org/ar/ |