Code

Opened 3 years ago

Closed 3 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.

Attachments (0)

Change History (2)

comment:1 Changed 3 years ago by aaugustin

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

Improved formatting.

comment:2 Changed 3 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)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.