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 .venv/lib/python3.12/site-packages/jinja2/filters.py
 994
 995
 996
 997
 998
 999
1000
1001
1002
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 .venv/lib/python3.12/site-packages/jinja2/filters.py
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
@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 .venv/lib/python3.12/site-packages/jinja2/filters.py
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
@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 .venv/lib/python3.12/site-packages/jinja2/filters.py
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
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 .venv/lib/python3.12/site-packages/jinja2/filters.py
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
@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