How to show absolute value of a number in Shopify

Since 2006, Liquid has been used in Shopify as a language template. In Liquid, you use tags consisting of comment, control flow, iteration, raw, and variable to show complex content. In today tutorial, we will tell you about math filters, especially abs value so that you are able to display math formulas.

Table of content

General information about Liquid

Liquid is the backbone language of Shopify and has been trusted by a lot of web developers. Liquid is one of the Shopify’s open-source template languages. The template has been sold since 2006 and now become a good source for a lot of hosted website application such as Jekyll, salesforce desk, and zendesk.

What is math filter

Math filters let you apply mathematical tasks. Math filters can be linked, and like other filters in Liquid, the computer will read math filter from left to right. In the example below, the computer will access minus then times and finally divided by.


You save {{ product.compare_at_price | minus: product.price | times: 100.0 | divided_by: product.compare_at_price }}%

How to show absolute value of a number

Math filters contain a lot of types, and this tutorial focuses on abs. Abs returns the absolute value of a number. You have already known about the absolute number when studying about math in high school, right? For instance, abs will generate a positive number.

Input


{{ -17 | abs }}

Output


17

In addition, abs also work on a string if the string only contains a number.

Input


{{ "-19.86" | abs }}

Output


19.86

Conclusion

To sum up, math filters help to apply mathematics formulas on your website. Abs is a value that produces positive number which is familiar with absolute number that you have known in your high school. We hope that you find this useful and let us know about your difficulties.