"""
`Path converters`_ used in URL patterns.
.. _`Path converters`:
https://docs.djangoproject.com/en/4.1/topics/http/urls/#path-converters
"""
[docs]class FloatConverter:
"""Path converter that matches a float number."""
#: The pattern to match.
regex = r'\d+(\.\d+)?'
[docs] def to_python(self, value: str) -> float:
"""
Convert the matched string into the type
that should be passed to the view function.
:param value: The matched string.
:return: The match as a ``float``.
"""
return float(value)
[docs] def to_url(self, value: str) -> str:
"""
Convert the Python type into a string to be used in the URL.
:param value: The matched string.
:return: The match with trailing zeroes removed.
"""
return f'{float(value):g}'
__all__ = ['FloatConverter']