Skip to content

abs

The abs filter is designed to calculate the absolute value of a number.

Functionality

  • Numbers: Takes a numerical value as input (integer or floating-point).
  • Absolute Value: Calculates the absolute value, which is the distance of the number from zero. It removes any negative sign and returns the positive magnitude of the number.
  • Output: Returns a new numerical value representing the absolute value of the input.

Syntax

    {{ input_number | abs }}
Arguments

The abs filter does not require any arguments.

Code Samples

Example 1: Absolute Value of a Negative Number

    {% assign temperature = -10 %}
    {{ temperature | abs }} 
Output:
10
Example 2: Absolute Value of a Positive Number
    {% assign profit = 1500.50 %}
    {{ profit | abs }}
Output:
1500.50
Example 3: Absolute Value of Zero
    {% assign zero_value = 0 %}

    {{ zero_value | abs }}

Output:

0

Outliers and Special Cases

  • Non-Numeric Input: If the input is not a number (e.g., a string or boolean), the abs filter might attempt to convert it to a number before calculating the absolute value. If the conversion fails, an error might be thrown, depending on Experience Builder's error handling.

Key Points

  • The abs filter is a simple but useful tool for mathematical operations within templates.
  • It is particularly helpful when dealing with values that could be positive or negative, and you only need the magnitude.
  • Remember that the original input number is not modified; a new number representing the absolute value is returned.