#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)
Change History (4)
by , 16 years ago
Attachment: | decimal_validator.diff added |
---|
comment:1 by , 16 years ago
Cc: | added |
---|
comment:2 by , 16 years ago
Resolution: | → wontfix |
---|---|
Status: | new → 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)."
Patch over the trunk (r8301).