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
1007
1008
1009
1010
1011
1012
1013
1014
1015
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
@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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
@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
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
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
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
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
@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