Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#17414 closed Bug (fixed)

intcomma raises ZeroDivisionError for locales whose NUMBER_GROUPING is unspecified

Reported by: jittat Owned by: nobody
Component: Internationalization Version: dev
Severity: Normal Keywords: ZeroDivisionError, intcomma
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When using locales that do not specify NUMBER_GROUPING (like 'fa','ja','th',) template filter intcomma (from humanize) raises ZeroDivisionError.

Version: Django 1.4 pre-alpha SVN-17202

How to reproduce:

  1. Make sure that USE_L10N = True in settings.py
  2. Try this code in shell
from django.utils import translation
translation.activate('th')
t = "{% load humanize %}}{{ 100|intcomma }}"
from django.template import Template, Context
Template(t).render(Context())
  1. You can change 'th' to 'ja', 'mn', 'ro' and many others.

Possible cause:

This is not the issue directly related to contrib.humanize's intcomma.

The exception occurs in function format in django.utils.numberformat when Django tries to divide something with parameter grouping. This parameter is assigned in function number_format in django.utils.formats from a call to get_format('NUMBER_GROUPING'), which returns 0 for many locales (e.g., th, ja, km, zh) with no definition of NUMBER_GROUPING in the format files. Therefore, you get ZeroDivisionError.

How to fix:

I am not sure where to put the fixing code. These are the options:

  • Use something like 3 as a default for NUMBER_GROUPING.
  • Change function format in django.utils.numberformat to make sure that grouping is non-zero (and if it is zero, use something like 3 as a default).

Since I am not sure where to put the fix, I have not tried to produce the patch. I can try to do that for the 2nd option if it makes sense.

Attachments (1)

17414-1.diff (1.6 KB ) - added by Claude Paroz 13 years ago.
Fix possible ZeroDivisionError in numberformat

Download all attachments as: .zip

Change History (4)

comment:1 by Claude Paroz, 13 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

comment:2 by Aymeric Augustin, 13 years ago

Resolution: fixed
Status: newclosed

In [17267]:

Fixed #17414 -- Prevented numberformat from trying to group digits when the number of digits per group is zero.

by Claude Paroz, 13 years ago

Attachment: 17414-1.diff added

Fix possible ZeroDivisionError in numberformat

comment:3 by Aymeric Augustin, 13 years ago

In [17268]:

Added a lower level test for numberformat when grouping is 0 and force_grouping is True. Thanks Claude Paroz. Refs #17414.

Note: See TracTickets for help on using tickets.
Back to Top