Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#8233 closed (wontfix)

Decimal Field can accept float value

Reported by: Michel Sabchuk Owned by: nobody
Component: Validators Version: master
Severity: Keywords:
Cc: michelts@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The Decimal Field accepts the following types:

  • a decimal.Decimal instance value;
  • a string representation of a float;

What about to accept float values too? The float value can be converted using the DecimalField format_number method, see the code above:

    def to_python(self, value):
        if value is None:
            return value
        try:
            # if the value is a float, convert it to a string and let decimal.Decimal do the work
            if isinstance(value, float):
                value = self.format_number(value)
            return decimal.Decimal(value)
        except decimal.InvalidOperation:
            raise validators.ValidationError(
                _("This value must be a decimal number."))

Attachments (1)

decimal_validator.diff (540 bytes) - added by Michel Sabchuk 8 years ago.
Patch over the trunk (r8301).

Download all attachments as: .zip

Change History (4)

Changed 8 years ago by Michel Sabchuk

Attachment: decimal_validator.diff added

Patch over the trunk (r8301).

comment:1 Changed 8 years ago by Michel Sabchuk

Cc: michelts@… added

comment:2 Changed 8 years ago by Jacob

Resolution: wontfix
Status: newclosed

The point of a Decimal type is that it doesn't have the precision problems that floats do. The Python decimal library correctly refuses to do this kind of conversion implicitly, and so should we. From the decimal docs:

"To create a Decimal from a float, first convert it to a string. This serves as an explicit reminder of the details of the conversion (including representation error)."

comment:3 Changed 8 years ago by (none)

milestone: 1.0 maybe

Milestone 1.0 maybe deleted

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