Opened 4 years ago

Closed 4 years ago

#16881 closed Uncategorized (invalid)

db.backends.util.format_number fails with valid inputs

Reported by: anonymous Owned by: nobody
Component: Uncategorized Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by aaugustin)

We see the error "quantize result has too many digits for current context" when using a DecimalField with max_digits=7 decimal_places=2 ie the following call fails

format_number(Decimal('914123.22'), 7, 2)

I believe this is due to the statement

context.prec = max_digits

whereas I think it should be

context.prec = max_digits + decimal_places

but I am not sure.

Change History (2)

comment:1 Changed 4 years ago by aaugustin

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Improved formatting.

comment:2 Changed 4 years ago by aaugustin

  • Resolution set to invalid
  • Status changed from new to closed

In fact, Django interprets max_digits as the total number of digits — before and after the decimal point.

See https://docs.djangoproject.com/en/dev/ref/models/fields/#decimalfield

So your example should be written like this:

format_number(Decimal('914123.22'), 8, 2)
Note: See TracTickets for help on using tickets.
Back to Top