Django and Comma-Formatting Numbers

996 views
Skip to first unread message

Greg Taylor

unread,
May 28, 2008, 7:26:52 PM5/28/08
to Django users
I was wondering if there was any facility within Django that makes
formatting numbers with commas or periods in templates possible from
template-land. I know you can use locale.format() on the Python side,
but was hoping there was some tag/filter feature I missed that would
allow me to do this without writing my own.

Russell Keith-Magee

unread,
May 28, 2008, 7:42:35 PM5/28/08
to django...@googlegroups.com

Sounds like you want the facilities provided by humaninze:

http://www.djangoproject.com/documentation/add_ons/#humanize

Yours,
Russ Magee %-)

Greg Taylor

unread,
May 28, 2008, 8:30:02 PM5/28/08
to Django users
Bingo, I could've sworn I remembered doing it somehow or another.

Thanks a lot,
Greg

On May 28, 7:42 pm, "Russell Keith-Magee" <freakboy3...@gmail.com>
wrote:

David

unread,
May 29, 2008, 2:15:56 PM5/29/08
to Django users
I had need of commas in floating-point numbers, and slightly modified
humanize.py with a floatcomma function:

def floatcomma(value):
"""
Converts a float to a string containing commas every three digits.
For example, 3000.65 becomes '3,000.65' and -45000.00 becomes
'-45,000.00'.
"""
orig = force_unicode(value)
intpart, dec = orig.split(".")
intpart = intcomma(intpart)
return ".".join([intpart, dec])
floatcomma.is_safe = True
register.filter(floatcomma)

James Bennett

unread,
May 29, 2008, 2:41:42 PM5/29/08
to django...@googlegroups.com
On Thu, May 29, 2008 at 1:15 PM, David <dhan...@arinc.com> wrote:
> I had need of commas in floating-point numbers, and slightly modified
> humanize.py with a floatcomma function:
>
> def floatcomma(value):
> """
> Converts a float to a string containing commas every three digits.
> For example, 3000.65 becomes '3,000.65' and -45000.00 becomes
> '-45,000.00'.
> """

Of course, keep in mind that this has internationalization issues;
there are places where the number "three thousand point six five" is
written "3000,65" or even "3.000,65".


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Reply all
Reply to author
Forward
0 new messages