Opened 2 hours ago

Last modified 2 hours ago

#37579 assigned Bug

numberformat.format() raises InvalidOperation for Decimal("NaN") with decimal_pos

Reported by: Shakil Ur Rehman Owned by: Shakil Ur Rehman
Component: Utilities Version: dev
Severity: Normal Keywords:
Cc: Shakil Ur Rehman Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

django.utils.numberformat.format() checks if not number.is_finite(): *after* comparing abs(number) < cutoff when decimal_pos is provided. Comparing Decimal("NaN") against cutoff raises decimal.InvalidOperation.

Minimal reproduction:

from decimal import Decimal
from django.conf import settings
from django.utils.numberformat import format as nformat

settings.configure(USE_I18N=False, USE_THOUSAND_SEPARATOR=False)
nformat(Decimal("NaN"), ".", decimal_pos=2)

Traceback:

  File "django/utils/numberformat.py", line 48, in format
    if abs(number) < cutoff:
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]

Expected behavior:
if not number.is_finite(): should be checked before the decimal_pos cutoff comparison so Decimal("NaN") returns "NaN" (matching decimal_pos=None).

Change History (3)

comment:1 by Shakil Ur Rehman, 2 hours ago

Cc: Shakil Ur Rehman added

comment:2 by Shakil Ur Rehman, 2 hours ago

Has patch: set

comment:3 by Shakil Ur Rehman, 2 hours ago

Owner: set to Shakil Ur Rehman
Status: new → assigned
Note: See TracTickets for help on using tickets.
Back to Top