Skip to content

times

The times filter multiplies a numeric value by a specified factor. It is a fundamental arithmetic operation for calculations within your templates.

Functionality

  • Numbers: Takes a numerical value as input (integer or floating-point).
  • Factor: Requires a numerical argument specifying the factor to multiply by.
  • Multiplication: Multiplies the input number by the factor.
  • Output: Returns a new numerical value representing the result of the multiplication.

Syntax

    {{ input_number | times: factor }}

Arguments

  • factor: The numerical value to multiply the input number by.

Code Samples

Example 1: Doubling a Number

    {% assign quantity = 5 %}
    {{ quantity | times: 2 }}
Output:
10
Example 2: Calculating a Discount
    {% assign original_price = 100 %}
    {% assign discount_percentage = 0.25 %}
    {{ original_price | times: discount_percentage }}

Output:

25.0
Example 3: Scaling a Value
    {% assign rating = 4 %}
    {{ rating | times: 1.5 }}
Output:
6.0

Outliers and Special Cases

  • Non-Numeric Input: If the input or the factor 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.
  • Multiplication by Zero: Multiplying any number by zero results in zero.
  • Multiplication by One: Multiplying any number by one returns the original number.

Key Points

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