Opened 13 years ago

Closed 13 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 Aymeric Augustin)

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 by Aymeric Augustin, 13 years ago

Description: modified (diff)

Improved formatting.

comment:2 by Aymeric Augustin, 13 years ago

Resolution: invalid
Status: newclosed

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