Opened 6 years ago

Closed 6 years ago

#29844 closed Bug (duplicate)

Cast DecimalField to FloatField rounds to integer on MySQL

Reported by: Ron Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When I cast a DecimalField to Float, the float value gets rounded which creates wrong results.

For example is decimalfield=75.75 and in the annotation I see 76 in decimalfield_float. Seen in django 1.11.15 on MariaDB.

Model:

class MyModel(models.Model):
 floatfield = models.FloatField('My FloatField')
 decimalfield = models.DecimalField(_("My DecimalField"), decimal_places=2, max_digits=12, validators=[MinValueValidator(Decimal('0.01'))])

Query:

MyModel.objects.all().annotate(decimalfield_float=Cast('decimalfield', FloatField()))

Change History (1)

comment:1 by Tim Graham, 6 years ago

Resolution: duplicate
Status: newclosed
Summary: djang orm cast DecimalField to FloatField forced roundingCast DecimalField to FloatField rounds to integer on MySQL

Duplicate of #28967 which is fixed in Django 2.1.

Note: See TracTickets for help on using tickets.
Back to Top