Opened 10 months ago

Last modified 7 months ago

#32559 assigned New feature

Add attribute 'step' to FloatField.

Reported by: Jacob Rief Owned by: Jacob Rief
Component: Forms Version: dev
Severity: Normal Keywords: FloatField, NumberInput, step
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Jacob Rief)

If someone wants to use the step attribute as provided by the HTML field
<input type="number" ...> , she/he has to specify that using for instance
FloatField(widget=NumberInput(attrs={'step': 0.5})).

Since the HTML standard offers a step attribute on input fields of type="number",
this feature shall be reflected by Django's FloatField and optionally DecimalField,
rather than having to parametrize the widget.

Min- and max-values are already supported by the FloatField, so the step-value
would make sense here as well. It furthermore would require to revalidate the
step-value by Django's Form validation, rather than by HTML alone.


Change History (13)

comment:1 Changed 10 months ago by Markus Holtermann

Triage Stage: UnreviewedAccepted

Thank you. Sounds like a perfectly valid use case.

comment:3 Changed 10 months ago by Kapil Bansal

Owner: changed from nobody to Kapil Bansal
Status: newassigned

comment:4 Changed 10 months ago by Kapil Bansal

Description: modified (diff)
Has patch: set
Last edited 10 months ago by Mariusz Felisiak (previous) (diff)

comment:5 Changed 10 months ago by Mariusz Felisiak

Summary: Add attribute 'step' to FloatField and DecimalFieldAdd attribute 'step' to FloatField.

comment:6 Changed 10 months ago by Mariusz Felisiak

Description: modified (diff)

comment:7 Changed 10 months ago by Mariusz Felisiak

Needs tests: set

comment:8 Changed 10 months ago by Kapil Bansal

Description: modified (diff)
Needs tests: unset

comment:9 Changed 8 months ago by Adam Johnson

On the forum, Kapil wrote:

I was working on ticket 32559 to add step in FloatField but how to write validation check for this.
Due to python floating point issues, I am not able to validate whether field value is of given step_size or not

The loss of precision in floating points is a real issue. The normal way to validate would be to use the modulo operator to check there is no remainder after dividing by the step, but this isn't possible in floating points with common decimal steps like 0.1:

>>> step = 0.1
>>> 1 % step == 0
>>> 1 % step

This problem cannot be solved for FloatField - I suggest we move the ticket to modify DecimalField, as Decimal objects do not have the same problem:

>>> from decimal import Decimal
>>> step = Decimal('0.1')
>>> Decimal(1) % step == 0
>>> Decimal(1) % step

We'd want to enforce that step is given as a Decimal.

The float problem can also occur when constructing a Decimal from a float:

>>> Decimal(0.1)

Perhaps we should have a check that the given step does not have more than 10 digits after the decimal point?

comment:10 Changed 8 months ago by Kapil Bansal

I got a reply on mailing list to use math.isclose function. Pull Request is already opened to be reviewed

comment:11 Changed 8 months ago by Jacob Rief

Description: modified (diff)

comment:12 Changed 8 months ago by Jacob Rief

Description: modified (diff)

I reviewed that pull request and after some testing I came to the conclusion, that math.isclose with a tolerance of 1e-9 is the best solution to fix the floating point rounding errors. All other approaches did not work properly or were far too complicated.

comment:13 Changed 7 months ago by Jacob Rief

Owner: changed from Kapil Bansal to Jacob Rief

Reclaiming ticket because of missing progress.

A new pull request is here:
This adds the missing documentation and release note for this feature.

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