Filters

Liquid Number Filters

Number Filters

Liquid number filters format Shopify numbers with round and money.

Understanding Liquid Number Filters

Liquid number filters are essential tools in Shopify that allow developers to format and manipulate numerical data within their store's Liquid templates. These filters can handle various tasks, such as rounding numbers or formatting currency, which are crucial for displaying prices and other numerical data accurately.

The Round Filter

The round filter is used to round a number to the nearest whole number or to a specified number of decimal places. This is particularly useful for displaying prices in a more appealing way.

To use the round filter, you simply append | round: decimal_places to your number variable. If decimal_places is not specified, it defaults to 0.

  • The first example rounds the product price to the nearest whole number.
  • The second example rounds the product price to two decimal places, which is common for displaying prices.

The Money Filter

The money filter formats numbers as currency. This filter is particularly useful in e-commerce sites where prices need to be presented as monetary values.

You can use the money filter by appending | money to your number variable. This automatically formats the number according to the store's currency settings.

The above example will display the product price in the currency format set in your Shopify store, adding currency symbols and separators as configured.

Combining Filters

Liquid allows combining filters to achieve more complex formatting. You can chain multiple filters to refine the formatting further. For instance, you can first round a number and then format it as currency.

Here's how you can combine the round and money filters:

This example first rounds the product price to two decimal places and then formats it as currency. This ensures that even if the original price had more decimals, it appears correctly formatted as per the store's settings.

Practical Examples of Number Filters

Let's look at a practical example where these number filters can significantly improve the readability of price data in a Shopify store:

Suppose you have a product with a base price of 19.9999 and you want to display it neatly formatted for your customers.

  • Original Price: Displays the raw price without any formatting.
  • Rounded Price: Rounds the price to two decimal places for clarity.
  • Formatted Price: Combines rounding and currency formatting for a professional display.

Conclusion

Liquid number filters are powerful tools for enhancing the presentation of numerical data in Shopify. By using filters like round and money, developers can ensure that prices and other numerical information are displayed in a user-friendly, professional manner. Mastering these filters will help you create a more polished and effective e-commerce site.