Opened 16 years ago

Closed 16 years ago

Last modified 16 years ago

#8233 closed (wontfix)

Decimal Field can accept float value

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

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 16 years ago.
Patch over the trunk (r8301).

Download all attachments as: .zip

Change History (4)

by Michel Sabchuk, 16 years ago

Attachment: decimal_validator.diff added

Patch over the trunk (r8301).

comment:1 by Michel Sabchuk, 16 years ago

Cc: michelts@… added

comment:2 by Jacob, 16 years ago

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 by (none), 16 years ago

milestone: 1.0 maybe

Milestone 1.0 maybe deleted

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