Skip to content

minus

The minus filter subtracts a specified number from 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).
  • Subtrahend: Requires a numerical argument specifying the number to subtract.
  • Subtraction: Subtracts the second number (the subtrahend) from the first number (the input).
  • Output: Returns a new numerical value representing the result of the subtraction.

Syntax

    {{ input_number | minus: subtrahend }}
Arguments

  • subtrahend: The numerical value to subtract from the input number.

Code Samples

Example 1: Subtracting a Positive Number

    {% assign quantity = 10 %}
    {{ quantity | minus: 3 }}
Output:
7
Example 2: Subtracting a Negative Number

    {% assign temperature = 5 %}
    {{ temperature | minus: -2 }} 

Output:

7
Example 3: Subtracting Decimals

    {% assign price = 15.99 %}
    {% assign discount = 2.50 %}
    {{ price | minus: discount }}
Output:
13.49

Outliers and Special Cases

  • Non-Numeric Input: If the input or the subtrahend 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 minus filter is a basic but essential arithmetic operation for working with numbers in templates.
  • It is useful for calculating differences, discounts, or any scenario where you need to subtract one value from another.
  • Pay attention to the order of the values: the subtrahend is subtracted from the input_number.
  • Be mindful of data types to ensure correct behavior and avoid errors.