
Update of /cvsroot/python/python/nondist/sandbox/decimal In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25435 Added Files: money.py Log Message: Draft of Money class. --- NEW FILE: money.py --- import Decimal Decimal.SetDefaultContext(Decimal.BASIC_DEFAULT_CONTEXT) context = Decimal.getcontext() context.prec = 20 context.rounding = Decimal.ROUND_HALF_UP # styles for currency simbol OUTSIDE = 'outside' # $-15.36 or $(15.36) INSIDE = 'inside' # -$15.36 or ($15.36) # styles for minus sign DASH = 'dash' # $-15.36 or -$15.36 PARENTHESES = 'parentheses' # $(15.36) or ($15.36) class Money(Decimal.Decimal): '''Money class''' decimalSeparator = '.' thousandSeparator = '' currencySymbol = '$' decimal_positions = 2 currSymbolStyle = OUTSIDE minusStyle = DASH def __init__(self, value=0): Decimal.Decimal.__init__(self, value) self.quantdec = Decimal.Decimal('1e-'+str(self.decimal_positions)) def __str__(self): (sign, number, exp) = self.quantize(self.quantdec).as_tuple() numbint = number[:exp] numbdec = number[exp:] # convert to list and reverse nint = list(numbint) nint.reverse() parts_lrev = [nint[x:x+3] for x in range(0, len(nint), 3)] parts_lrev.reverse() # reverse each chunk parts_str = [] for part in parts_lrev: part.reverse() parts_str.append(''.join(map(str, part))) # parts of the final string cSS = self.currSymbolStyle mS = self.minusStyle if sign: if cSS == OUTSIDE and mS == DASH: fstr = [self.currencySymbol, '-'] elif cSS == INSIDE and mS == DASH: fstr = ['-', self.currencySymbol] elif cSS == OUTSIDE and mS == PARENTHESES: fstr = [self.currencySymbol, '('] elif cSS == INSIDE and mS == PARENTHESES: fstr = ['(', self.currencySymbol] else: raise ValueError, "Bad value for minus or currency symbol styles!" else: fstr = [self.currencySymbol] fstr.append(self.thousandSeparator.join(parts_str)) fstr.append(self.decimalSeparator) fstr.extend(map(str, numbdec)) if sign and mS == PARENTHESES: fstr.append(')') return ''.join(fstr) def __repr__(self): return "Money('%s')" % Decimal.Decimal.__str__(self)
participants (1)
-
facundobatistaļ¼ users.sourceforge.net