Re: numeric formatting

2006-03-27 Thread jtm

Quick and nasty:

from django.core import template
register = template.Library()

@register.filter(name='commas')
def commas(value):
return "".join(commafy(value))

def commafy(s):
pieces = s.split(".")
l = len(pieces[0])
for i in range(0,l):
if (l - i) % 3 or not i:
yield pieces[0][i]
else:
yield ","
yield pieces[0][i]
if len(pieces) > 1:
yield "." + pieces[1]

value should be a string (eg your preformatted number).
obviously does not care about your locale.

jtm


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: numeric formatting

2006-03-24 Thread Eric Walstad

On Friday 24 March 2006 12:23, Jacob Kaplan-Moss wrote:
> > I don't think that python built-in formatting can do this. (Am I
> > wrong?)
>
> Actually, locale.format will do it for you, IIRC.

>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'en_US.UTF-8'
>>> locale.format("%d", 1234567, True)
'1,234,567'

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: numeric formatting

2006-03-24 Thread Jacob Kaplan-Moss

On Mar 24, 2006, at 1:48 PM, Rock wrote:
> Is there a template filter for turning a large integer into a human
> readable number?
>
> 1234567 --> 1,234,567
> 12345   --> 12,345
>
> I don't think that python built-in formatting can do this. (Am I
> wrong?)

Actually, locale.format will do it for you, IIRC.

But there's not a Django filter for it (probably should be, though...)

> Assuming there isn't already a simple filter that will do this,  
> what do
> I need to learn about in order to create my own filter?

You want the template language for Python programmers doc, and  
specificaly the part about writing filters: http:// 
www.djangoproject.com/documentation/templates_python/#writing-custom- 
template-filters

Jacob

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---