at_most¶
The at_most filter ensures that a numeric value does not exceed a specified maximum value. It is useful for setting upper bounds or limits on values in your Experience Builder templates.
Functionality
- Numbers: Takes a numerical value as input (integer or floating-point).
- Maximum Value: Requires a numerical argument specifying the maximum allowed value.
- Comparison: Compares the input number to the maximum value.
- Output:
- If the input number is less than or equal to the maximum value, it returns the input number unchanged.
- If the input number is greater than the maximum value, it returns the maximum value.
Syntax
Arguments- maximum_value: The highest acceptable numerical value. If the input number is above this value, the filter will output this
maximum_value.
Code Samples
Example 1: Limiting a Discount Percentage
{% assign discount_percentage = 25 %}
{% assign max_discount = 20 %}
{{ discount_percentage | at_most: max_discount }}
{% assign player_score = 120 %}
{% assign max_score = 100 %}
{{ player_score | at_most: max_score }}
Output:
Example 3: No Change if Below Maximum Output:Outliers and Special Cases¶
- Non-Numeric Input: If the input is not a number (e.g., a string or boolean), the
at_mostfilter might attempt to convert it to a number before comparison. If the conversion fails, an error might be thrown. - Maximum Value as String: If the
maximum_valueargument is provided as a string, it needs to be convertible to a number for the comparison to work correctly.
Key Points¶
- The
at_mostfilter provides a simple way to enforce upper limits on numerical values in your templates. - It is useful for scenarios where you want to ensure that a value doesn't exceed a certain threshold.
- Use it to cap scores, limit percentages, or control the upper end of a range of values.
- Be mindful of data types to ensure correct behavior and avoid errors.