FloatField
This class represents a form field for floating-point number input, extending the functionality of an integer field to handle decimal values. It validates that inputs can be converted to floats, ensures values are finite, and automatically manages widget attributes like step size for numeric inputs. The field also supports localized number formatting and handles empty values by returning None.
Attributes
| Attribute | Type | Description |
|---|---|---|
| default_error_messages | dict = {"invalid": _("Enter a number.")} | A dictionary containing the default error message for the 'invalid' key, used when the input cannot be converted to a float or is not a finite number. |
Methods
to_python()
@classmethod
def to_python(
value: any
) - > float
Validate that float() can be called on the input. Return the result of float() or None for empty values.
Parameters
| Name | Type | Description |
|---|---|---|
| value | any | The raw input value to be converted into a float, which may be localized or empty |
Returns
| Type | Description |
|---|---|
float | The converted floating-point number, or None if the input is an empty value |
validate()
@classmethod
def validate(
value: float
) - > null
Validates that the input value is a finite number and not NaN or infinity.
Parameters
| Name | Type | Description |
|---|---|---|
| value | float | The floating-point number to check for finiteness |
Returns
| Type | Description |
|---|---|
null | Nothing is returned, but a ValidationError is raised if the value is non-finite |
widget_attrs()
@classmethod
def widget_attrs(
widget: [Widget](../widgets/widget.md?sid=django_forms_widgets_widget)
) - > dict
Builds a dictionary of HTML attributes for the form widget, specifically setting the 'step' attribute for numeric inputs.
Parameters
| Name | Type | Description |
|---|---|---|
| widget | [Widget](../widgets/widget.md?sid=django_forms_widgets_widget) | The form widget instance for which attributes are being generated |
Returns
| Type | Description |
|---|---|
dict | A dictionary of HTML attributes including 'step' configuration for the browser widget |