Skip to content

number (6)

abs

abs(x, /)

Return the absolute value of the argument.

Example

Jinja call:

{{ -5 | abs }}
Result: 5

Example

Jinja call:

{{ [-1, -5, 10] | map('abs') | list }}
Result: [1, 5, 10]

DocStrings

float

float(value: Any, default: float = 0.0) -> float

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 jinja2/filters.py
def do_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:
        return float(value)
    except (TypeError, ValueError):
        return default

max

max(environment: 'Environment', value: 't.Iterable[V]', case_sensitive: bool = False, attribute: Union[str, int, NoneType] = None) -> 't.Union[V, Undefined]'

Return the largest item from the sequence.

Example

Jinja call:

{{ [1, 2, 3] | max }}
Result: 3

Example

Jinja call:

{{ [{"val": 1}, {"val": 2}] | max(attribute="val") }}
Result: {'val': 2}

DocStrings
Source code in jinja2/filters.py
@pass_environment
def do_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)

min

min(environment: 'Environment', value: 't.Iterable[V]', case_sensitive: bool = False, attribute: Union[str, int, NoneType] = None) -> 't.Union[V, Undefined]'

Return the smallest item from the sequence.

Example

Jinja call:

{{ [1, 2, 3] | min }}
Result: 1

Example

Jinja call:

{{ [{"val": 1}, {"val": 2}] | min(attribute="val") }}
Result: {'val': 1}

DocStrings
Source code in jinja2/filters.py
@pass_environment
def do_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)

round

round(value: float, precision: int = 0, method: 'te.Literal["common", "ceil", "floor"]' = 'common') -> float

Round the number to a given precision. The first

Example

Jinja call:

{{ 1.6 | round }}
Result: 2.0

Example

Jinja call:

{{ 1.23456 | round(2) }}
Result: 1.23

DocStrings
Source code in jinja2/filters.py
def do_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
    """
    if method not in {"common", "ceil", "floor"}:
        raise FilterArgumentError("method must be common, ceil or floor")

    if method == "common":
        return round(value, precision)

    func = getattr(math, method)
    return t.cast(float, func(value * (10**precision)) / (10**precision))

sum

sum(environment: 'Environment', iterable: 't.Iterable[V]', attribute: Union[str, int, NoneType] = None, start: ~V = 0) -> ~V

Returns the sum of a sequence of numbers plus the value of parameter

Example

Jinja call:

{{ [1, 2, 3] | sum }}
Result: 6

DocStrings
Source code in jinja2/filters.py
@async_variant(sync_do_sum)  # type: ignore
async def do_sum(
    environment: "Environment",
    iterable: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
    attribute: t.Optional[t.Union[str, int]] = None,
    start: V = 0,  # type: ignore
) -> V:
    rv = start

    if attribute is not None:
        func = make_attrgetter(environment, attribute)
    else:

        def func(x: V) -> V:
            return x

    async for item in auto_aiter(iterable):
        rv += func(item)

    return rv