Opened 9 years ago

Last modified 5 weeks ago

#26459 assigned New feature

Allow providing DecimalField with a custom context

Reported by: yasondinalt Owned by: Daniel Rios
Component: Database layer (models, ORM) Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

DecimalField has weird behavior in case of float values passed to field.
If decimal_places = 1
float 2.15 will be saved as 2.1 (!)
float 2.25 will be saved as 2.2 (!)
But if float value first converted to str:
float 2.15 will be saved as 0.2 (ok)
float 2.25 will be saved as 0.2 (!)
It's because of default decimal rounding ROUND_HALF_EVEN.
As I understand build-in round() use rounding similar to ROUND_HALF_UP.

I tried first cast float to str, then use ROUND_HALF_UP, so now:
float 2.15 will be saved as 0.2 (ok)
float 2.25 will be saved as 0.3 (ok)

Attachments (2)

0001-tests-format-float-to-Decimal-with-proper-rounding.patch (1.1 KB ) - added by yasondinalt 9 years ago.
Tests
0001-DecimalField-rounding-first-cast-to-str-then-use-ROU.patch (1.5 KB ) - added by yasondinalt 9 years ago.
Changes

Download all attachments as: .zip

Change History (10)

comment:1 by yasondinalt, 9 years ago

Needs tests: set

comment:2 by Tim Graham, 9 years ago

Could you give a snippet of the application code where you run into this issue?

in reply to:  2 comment:3 by yasondinalt, 9 years ago

Replying to timgraham:

Could you give a snippet of the application code where you run into this issue?

class Invoice(models.Model):
    # same as django-paypal use
    mc_gross = models.DecimalField(max_digits=64, decimal_places=2, default=0)
    
invoice.mc_gross = 2.215 # float
invoice.save()

In DB will be value 2.21 (or if 2.225 -> 2.22)

Last edited 9 years ago by yasondinalt (previous) (diff)

comment:4 by Tim Graham, 9 years ago

Needs tests: unset

I'm not sure whether or not to accept the ticket so I asked for advice on the django-developers mailing list.

p.s. You don't need to attach patches on the ticket when you also send a pull request.

comment:5 by Tim Graham, 9 years ago

Has patch: unset
Summary: DecimalField float roundingAllow providing DecimalField with a custom context
Triage Stage: UnreviewedAccepted
Type: BugNew feature

Accepting per Aymeric's proposal on the mailing list:

Currently DecimalField accepts max_digits and decimal_places options. I think it should accept a decimal context and delegate all operations to that context.

I suggest the following behavior:

  • start with the decimal context provided in a kwarg to DecimalField or, if there is None, the current context returned by getcontext().
  • modify that context to take into account max_digits and decimal_places
  • ask the context to perform whatever operations we need

comment:6 by Mariusz Felisiak, 4 years ago

#31841 was marked as a duplicate.

comment:7 by Mariusz Felisiak, 4 years ago

#32555 was marked as a duplicate for ArrayField(DecimalField()).

comment:8 by Daniel Rios, 5 weeks ago

Owner: changed from nobody to Daniel Rios
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top