Forum Discussion

Bonfire-Champ's avatar
Bonfire-Champ
Active Member II
2 years ago

complex Liquid with percentages

Calling all Liquid Masters ðŸ‘€
(prob my use case is not even that complex, but bear with me)
Is it possible to create a Liquid syntax that will spit out a result which is the percentile difference between 2 values?
Let's say there's an attribute called 'last_week_total_meals' with value 5 and another one 'this_week_total_meals' with a value of 3, what liquid can I code to show that there's a -40% difference between both values?

  • Bonfire-Champ's avatar
    Bonfire-Champ
    Active Member II

    {% assign original_number = 5 | plus: 0.0 %} {% assign new_number = {{custom_attribute.${weekly_total_breakfast_tracked}}} | plus: 0.0 %} {% if new_number >= original_number %} -- INCREASED VALUE --

    {% assign difference_absolute = new_number | minus: original_number %} Absolute difference: {{difference_absolute | round}}

    {% assign difference_percentage = difference_absolute | divided_by: original_number | times: 100 %} Difference in percentage: +{{difference_percentage | round}}% {% elsif new_number < original_number %} -- DECREASED VALUE --

    {% assign difference_absolute = original_number | minus: new_number %} Absolute difference: {{difference_absolute | round}}

    {% assign difference_percentage = difference_absolute | divided_by: original_number | times: 100 %} Difference in percentage: -{{difference_percentage | round}}% {% else %} {% endif %}