The next function is a bit faster

import re
def formatAsDollars(amount):
  part = ("%.2f"%amount).split(".")
  part[0] = re.sub(r'\B(?=(...)*$)',',',part[0])
  return '$'+'.'.join(part)


2008/12/24 Joel Odom <joelo...@gmail.com>:
> Here's a little function I wrote that you may have:
>
> def formatAsDollars(amount):
>   dollars = int(amount)
>   cents = amount - dollars
>
>   i = 0
>   reverseDollarPlaces = []
>   for p in str(dollars)[::-1]:
>     if i % 3 == 0 and i:
>       reverseDollarPlaces.append(',')
>     reverseDollarPlaces.append(p)
>     i += 1
>
>   return '$%s.%.2i' % (''.join(reverseDollarPlaces[::-1]), int(100*cents))

> On Tue, Dec 23, 2008 at 2:32 PM, Shay Ben Dov <shay.ben...@gmail.com> wrote:
>>
>> Hi,
>>
>> I know that if
>>
>> sum = 560000.0
>>
>> then
>>
>> {{ sum|floatformat:2 }} will display 560000.00
>>
>> How we get $560,000.00
>>
>> Thanks,
>>
>> Shay Ben Dov
> --
> http://giscoder.blogspot.com/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to