Number Filters

These are provided in the default filters. Specifically, when TemplateOptions is constructed, these are added via the extension method WithNumberFilters.

Contents

    abs

    Returns the absolute value – the distance from zero – for the input number.

    {{ -50 | abs }}
    {{ 50 | abs }}
    50
    50

    at_least

    Returns the greater of the input and first argument. The exact opposite of at_most.

    {{ 10 | at_least:20 }}
    {{ 30 | at_least:20 }}
    20
    30

    at_most

    Returns the lesser of the input and first argument. The exact opposite of at_least.

    {{ 10 | at_most:20 }}
    {{ 30 | at_most:20 }}
    10
    20

    ceil

    The input number, rounded up to the next integer. (Remember, a NumberValue is a decimal internally.)

    {{ 10.23 | ceil }}
    11

    divided_by

    Divides the input by the argument.

    If the argument is an integer, the result is reduced to the nearest integer. (This is a duplicated quirk of the original Liquid.)

    {{ 10 | divided_by:2 }}
    {{ 10 | divided_by:3 }}
    5
    3

    To get a decimal back (a non-rounded result), you need to pass in a decimal.

    {{ 10 | divided_by:3.0 }}
    3.3333333333333333333333333333

    floor

    Reduces a decimal to the nearest smaller integer. (Note that NumberValue still wraps a decimal internally.)

    {{ 3.3 | floor }}
    3

    minus

    Subtracts the first argument from the input.

    {{ 10 | minus:5 }}
    {{ 10.1 | minus:5.3 }}
    5
    4.8

    modulo

    Divides the input by the first argument and returns the remainder.

    Both the input and the argument have to be integers (this isn’t a Liquid rule, this is a math rule). They will be converted to integers via Convert.ToInt32, so if you attempt to perform the operation on a decimal argument that converts to zero, you will get a “Attempted to divide by zero” error.

    {{ 10 | modulo:3 }}
    1

    (If you think you’ll be checking for multiples often, there’s an example of a custom ismultipleof operator in the chapter on conditionals).

    plus

    Adds the input and the argument.

    {{ 10.1 | plus:5.3 }}
    15.4

    round

    Rounds the input. Values at .5 round up. Takes an optional parameter for the number of decimal places. Defaults to 0.

    {{ 1.1 | round }}
    {{ 1.49 | round }}
    {{ 1.5 | round }}
    {{ 1.617 | round:2 }}
    1
    1
    2
    1.62

    times

    Multiples the input by the first argument.

    {{ 1.5 | times:2.5 }}
    3.75

    This is item #4 in a sequence of 5 items.

    You can use your left/right arrow keys or swipe left/right to navigate