How can I round off sales badge percentage to the nearest 5?

Topic summary

Main issue: Show discount percentages on sales badges rounded to the nearest 5% (e.g., 12% → 10%, 14.7% → 15) in Shopify.

Proposed solution (Liquid): Calculate the discount percentage, then round to the nearest 5 using divide/round/multiply:

  • Compute discount = (compare_at_price_max − price) / compare_at_price_max × 100.
  • Round to nearest 5 with: rounded_discount = discount | divided_by: 5 | round | times: 5.
  • Output: {{ rounded_discount }}% OFF.

Outcome: The provided Liquid snippet worked for the original poster on product pages.

Open point: Another participant reports it only works on product pages and asks for a version for collection pages; no solution provided yet.

Notes:

  • The core logic relies on Liquid filters (divided_by, round, times) to achieve nearest-5 rounding.
  • The code snippet is central to understanding the fix. The thread currently lacks guidance for collection page implementation, so the discussion remains open on that point.
Summarized with AI on December 18. AI used: gpt-5.

Hey @kathi8171

{%- if show_badges -%}

  {%- assign discount = product.compare_at_price_max | minus: product.price | times: 100.0 | divided_by: product.compare_at_price_max -%}
  {%- assign rounded_discount = discount | divided_by: 5 | round | times: 5 -%}
  {{ rounded_discount }}% OFF

{%- endif -%}
1 Like