Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#8233 closed (wontfix)

Decimal Field can accept float value

Reported by: michelts 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 michelts 7 years ago.
Patch over the trunk (r8301).

Download all attachments as: .zip

Change History (4)

Changed 7 years ago by michelts

Patch over the trunk (r8301).

comment:1 Changed 7 years ago by michelts

  • Cc michelts@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 7 years ago by jacob

  • Resolution set to wontfix
  • Status changed from new to closed

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 7 years ago by anonymous

  • milestone 1.0 maybe deleted

Milestone 1.0 maybe deleted

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