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
Arguments
- factor: The numerical value to multiply the input number by.
Code Samples
Example 1: Doubling a Number
Output: Example 2: Calculating a Discount {% assign original_price = 100 %}
{% assign discount_percentage = 0.25 %}
{{ original_price | times: discount_percentage }}
Output:
Example 3: Scaling a Value Output: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
timesfilter 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.