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)
--
Ticket URL: <https://code.djangoproject.com/ticket/26459>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* Attachment "0001-tests-format-float-to-Decimal-with-proper-
rounding.patch" added.
Tests
* Attachment "0001-DecimalField-rounding-first-cast-to-str-then-use-
ROU.patch" added.
Changes
* needs_better_patch: => 0
* needs_tests: => 1
* needs_docs: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/26459#comment:1>
* Attachment "0001-tests-format-float-to-Decimal-with-proper-
rounding.patch" added.
Tests
--
Comment (by timgraham):
Could you give a snippet of the application code where you run into this
issue?
--
Ticket URL: <https://code.djangoproject.com/ticket/26459#comment:2>
Comment (by yasondinalt):
Replying to [comment:2 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 = 10.215 # float
invoice.save()
}}}
In DB will be value 10.21 (or if 10.225 -> 10.22)
--
Ticket URL: <https://code.djangoproject.com/ticket/26459#comment:3>
* Attachment "0001-DecimalField-rounding-first-cast-to-str-then-use-
ROU.patch" added.
Changes
* needs_tests: 1 => 0
Comment:
I'm not sure whether or not to accept the ticket so I asked for advice on
the [https://groups.google.com/d/topic/django-
developers/bnoVTOx2GFs/discussion django-developers mailing list].
p.s. You don't need to attach patches on the ticket when you also send a
[https://github.com/django/django/pull/6410 pull request].
--
Ticket URL: <https://code.djangoproject.com/ticket/26459#comment:4>
* has_patch: 1 => 0
* stage: Unreviewed => Accepted
* type: Bug => New feature
Comment:
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
--
Ticket URL: <https://code.djangoproject.com/ticket/26459#comment:5>
Comment (by felixxm):
#31841 was marked as a duplicate.
--
Ticket URL: <https://code.djangoproject.com/ticket/26459#comment:6>
Comment (by Mariusz Felisiak):
#32555 was marked as a duplicate for `ArrayField(DecimalField())`.
--
Ticket URL: <https://code.djangoproject.com/ticket/26459#comment:7>