Skip to content

plus

The plus filter adds a specified number to the input number. It is a fundamental arithmetic operation for calculations within your templates.

Functionality

  • Numbers: Takes a numerical value as input (integer or floating-point).
  • Addend: Requires a numerical argument specifying the number to add.
  • Addition: Adds the second number (the addend) to the first number (the input).
  • Output: Returns a new numerical value representing the result of the addition.

Syntax

    {{ input_number | plus: addend }}

Arguments

  • addend: The numerical value to add to the input number.

Code Samples

Example 1: Adding a Positive Number

    {% assign quantity = 10 %}
    {{ quantity | plus: 3 }}
Output:
13
Example 2: Adding a Negative Number
    {% assign temperature = 5 %}
    {{ temperature | plus: -2 }} 
Output:
3
Example 3: Adding Decimals
    {% assign price = 15.99 %}
    {% assign shipping = 4.50 %}
    {{ price | plus: shipping }}
Output:
20.49

Outliers and Special Cases

  • Non-Numeric Input: If the input or the addend is not a number (e.g., a string), the filter might attempt to convert it to a number. If the conversion fails, an error might be thrown.

Key Points

  • The plus filter is a basic but essential arithmetic operation for working with numbers in templates.
  • It is useful for calculating totals, aggregating values, or any scenario where you need to add one value to another.
  • The order of the values does not matter in addition.
  • Be mindful of data types to ensure correct behavior and avoid errors.