Skip to content

floor

The floor filter rounds a numeric value down to the nearest integer. It is the opposite of the ceil filter and always returns the greatest integer less than or equal to the input value.

Functionality

  • Numbers: Takes a numerical value as input (integer or floating-point).
  • Rounding Down: Rounds the input number towards negative infinity, to the next whole number.
  • Output: Returns a new integer value representing the rounded result.

Syntax

    {{ input_number | floor }}
Arguments

The floor filter does not require any arguments.

Code Samples

Example 1: Rounding Down a Decimal

    {% assign price = 12.99 %}
    {{ price | floor }}

Output:

12
Example 2: No Change for Integers

    {% assign quantity = 5 %}
    {{ quantity | floor }}

Output:

5
Example 3: Rounding Down a Negative Decimal

    {% assign temperature = -7.2 %}
    {{ temperature | floor }}

Output:

-8

Outliers and Special Cases

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

Key Points

  • The floor filter is a basic mathematical operation for rounding numbers within templates.
  • It is useful when you want to ensure a value does not exceed a certain integer threshold.
  • Use cases include calculating maximum capacities, truncating timestamps, or any situation where you need to round down to the nearest whole number.
  • Be aware of data types to prevent errors and ensure expected behavior.