Someone on fr.comp.lang.python asked for a way to format a float as a french currency (i.e. with thousands separator). There doesn't seem to be anything available in the standard library and the % operator has no specific format for currency. Here's the code I came up with:
def as_currency(amount, thousand_sep=' ', decimal_sep=',', symbol='¤'): num = '%.2f'%amount left, right = num.split('.') left_digits = list(left) spaced_digits = [decimal_sep, right, symbol ] while left_digits: spaced_digits = [thousand_sep] + left_digits[-3:] + spaced_digits del left_digits[-3:] return ''.join(spaced_digits[1:])
This could probably be enhanced by using the locale module to get the values. It's rather unfortunate that locale.format has not % substitution for currencies.