Opened 7 years ago

Closed 7 years ago

Last modified 7 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: master
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


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
  2. Try this code in shell
from django.utils import translation
t = "{% load humanize %}}{{ 100|intcomma }}"
from django.template import Template, 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 7 years ago.
Fix possible ZeroDivisionError in numberformat

Download all attachments as: .zip

Change History (4)

comment:1 Changed 7 years ago by Claude Paroz

Needs tests: set
Triage Stage: UnreviewedAccepted

comment:2 Changed 7 years ago by Aymeric Augustin

Resolution: fixed
Status: newclosed

In [17267]:

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

Changed 7 years ago by Claude Paroz

Attachment: 17414-1.diff added

Fix possible ZeroDivisionError in numberformat

comment:3 Changed 7 years ago by Aymeric Augustin

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