Sure, but it is very poor code. #!/usr/bin/env python # -*- coding: iso-8859-15 -*- import locale from types import *
locale.setlocale(locale.LC_ALL, '') localenv = locale.localeconv() def commify(n): if n is None: return None if type(n) is StringType: sepdec = localenv['mon_decimal_point'] else: #if n is python float number we use everytime the dot sepdec = '.' n = str(n) if sepdec in n: dollars, cents = n.split(sepdec) else: dollars, cents = n, None r = [] for i, c in enumerate(reversed(str(dollars))): if i and (not (i % 3)): r.insert(0, localenv['mon_thousands_sep']) r.insert(0, c) out = ''.join(r) if cents: out += localenv['mon_decimal_point'] + cents return out def subst(str, pattern, fill): out = [] ctrlchars = 'X' + localenv['mon_thousands_sep'] for c in pattern: if str != '' and str != []: if c == "X": out.append(str[0]) str = str[1:] else: out.append(c) else: if c in ctrlchars: out.append(fill) else: out.append(c) return out def numify(string): ctrlchars = '0123456' + localenv['mon_decimal_point'] return ''.join([c for c in str(string) if c in ctrlchars]) def moneyfy(num, pattern): #if n is a float we must stringify and normalize the dec separator if type(num) is not StringType: num = str(num) num = num.replace('.',localenv['mon_decimal_point']) #we need integer part and decimal part od number and pattern try: (strint, strdec) = num.split(localenv['mon_decimal_point']) except: strint = num strdec = '' try: (patint, patdec) = pattern.split(localenv ['mon_decimal_point']) except: patint = pattern patdec = '' #inizialize buff0 = buff1 = sep = '' #decimal part must accomodate to pattern with correct zerofill #and patterm can have more lenght than number if len(strdec) == len(patdec): out = subst(strdec, patdec,'0') buff1 = ''.join(out) elif len(strdec) > len(patdec): strdec = strdec[:len(patdec)] if strdec != '': out = subst(strdec, patdec,'0') buff1 = ''.join(out) elif len(strdec) < len(patdec): #decimal part of money is less than pattern pattern = ['X'] * len(patdec) out = subst(strdec, pattern,'0') buff1 = ''.join(out) #integer part is composed by end... s = list(strint) s.reverse() p = list(patint) p.reverse() out = subst(s, p,' ') #....and is reversed for output out.reverse() buff0 = ''.join(out) #if pattern has decimals localize the decimal separator if patdec != '': sep = localenv['mon_decimal_point'] return buff0 + sep + buff1 if __name__ == '__main__': print commify(1) print commify(123) print commify(1234) print commify(1234567890) #....decimal....... print commify('123,0') print commify('1234,5') print commify('1234,56789') #....but.... print commify(123.0) print commify(1234.5) print commify(1234.56789) #....money as string.... print moneyfy('123456,1234', "€ XXX.XXX.XXX,XX") print moneyfy('123456,12', "€ XXX.XXX.XXX,XX") print moneyfy('123456,5', "€ XXX.XXX.XXX,XX") #....money as float..... print moneyfy(123456.1234, "€ XXX.XXX.XXX,XX") print moneyfy(123456.12, "€ XXX.XXX.XXX,XX") print moneyfy(123456.5, "€ XXX.XXX.XXX,XX") #....return number.... print numify("$ 123.456,50") On 29 Gen, 18:53, Aaron Swartz <m...@aaronsw.com> wrote: > can you share? > > On Jan 29, 2009 12:47 PM, "leone" <handja...@gmail.com> wrote: > > Thanks for your suggest. I used local module. > I have built a moneyfy function too. > Best regards > > On 29 Gen, 08:16, Aaron Swartz <m...@aaronsw.com> wrote: > > > won't the locale module tell you these? we should use that instead of > > having > our own config > > ... > > > On Jan 29, 2009 2:13 AM, "leone" <handja...@gmail.com> wrote: > > Some > > european countries use deci... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "web.py" group. To post to this group, send email to webpy@googlegroups.com To unsubscribe from this group, send email to webpy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/webpy?hl=en -~----------~----~----~----~------~----~------~--~---