Ad

How To Prevent From Rounding Numbers In Liquid

- 1 answer

I'm trying to calculate growth rate between two months and I'm getting round numbers instead of floating. Is there a way to prevent liquid from rounding numbers?

{{ articles_created_this_month_count | minus: articles_created_last_month_count | divided_by: articles_created_last_month_count | times: 100 }}%

{{ articles_created_this_month_count | minus: articles_created_last_month_count | divided_by: articles_created_last_month_count | times: 100 }}%

Ad

Answer

As with many programming languages, when Liquid divides an integer with an integer, the result will always be an integer again. So 5 / 2 will equal 2. But 5.0 / 2 will be 2.5. So you could multiple your articles_created_this_month_count with 1.0:

{{ articles_created_this_month_count | times: 1.0 | minus: articles_created_last_month_count | divided_by: articles_created_last_month_count | times: 100 }}
Ad
source: stackoverflow.com
Ad