Convert the value into a floating point number. If the
Example
Jinja call:
{{"0.4"|float}}
Result: 0.4
Example
Jinja call:
{{"invalid"|float(default=1.0)}}
Result: 1.0
DocStrings
Source code in .venv/lib/python3.12/site-packages/jinja2/filters.py
994 995 996 997 998 999100010011002
defdo_float(value:t.Any,default:float=0.0)->float:"""Convert the value into a floating point number. If the conversion doesn't work it will return ``0.0``. You can override this default using the first parameter. """try:returnfloat(value)except(TypeError,ValueError):returndefault
@pass_environmentdefdo_max(environment:"Environment",value:"t.Iterable[V]",case_sensitive:bool=False,attribute:t.Optional[t.Union[str,int]]=None,)->"t.Union[V, Undefined]":"""Return the largest item from the sequence. .. sourcecode:: jinja {{ [1, 2, 3]|max }} -> 3 :param case_sensitive: Treat upper and lower case strings as distinct. :param attribute: Get the object with the max value of this attribute. """return_min_or_max(environment,value,max,case_sensitive,attribute)
@pass_environmentdefdo_min(environment:"Environment",value:"t.Iterable[V]",case_sensitive:bool=False,attribute:t.Optional[t.Union[str,int]]=None,)->"t.Union[V, Undefined]":"""Return the smallest item from the sequence. .. sourcecode:: jinja {{ [1, 2, 3]|min }} -> 1 :param case_sensitive: Treat upper and lower case strings as distinct. :param attribute: Get the object with the min value of this attribute. """return_min_or_max(environment,value,min,case_sensitive,attribute)
defdo_round(value:float,precision:int=0,method:'te.Literal["common", "ceil", "floor"]'="common",)->float:"""Round the number to a given precision. The first parameter specifies the precision (default is ``0``), the second the rounding method: - ``'common'`` rounds either up or down - ``'ceil'`` always rounds up - ``'floor'`` always rounds down If you don't specify a method ``'common'`` is used. .. sourcecode:: jinja {{ 42.55|round }} -> 43.0 {{ 42.55|round(1, 'floor') }} -> 42.5 Note that even if rounded to 0 precision, a float is returned. If you need a real integer, pipe it through `int`: .. sourcecode:: jinja {{ 42.55|round|int }} -> 43 """ifmethodnotin{"common","ceil","floor"}:raiseFilterArgumentError("method must be common, ceil or floor")ifmethod=="common":returnround(value,precision)func=getattr(math,method)returnt.cast(float,func(value*(10**precision))/(10**precision))