assign type casted string to int Liquid variables for packing slips

Topic summary

Main issue: Casting a string to an integer in Liquid via assign didn’t persist as a numeric type, causing an error when comparing to 0 (“comparison of String with 0 failed”).

Tried approaches:

  • Assign with to_i: {% assign multi_qty = multi_qty_str | to_i %} then compare: {% if multi_qty > 0 %} → error.
  • Recasting in the condition worked: {% if multi_qty | to_i > 0 %} but was undesirable to repeat.
  • Parentheses and adding + 0 didn’t resolve the type issue.

Resolution: Applying abs after to_i during assignment fixes type for comparisons:

  • {% assign multi_qty = multi_qty_str | to_i | abs %}

Outcome: Problem solved; the variable can be compared numerically without re-casting in the condition. Status: resolved.

Summarized with AI on December 20. AI used: gpt-5.

Im trying to assign a variable that has been converted from a string but the result variable stays as a string

{% assign multi_qty = multi_qty_str | to_i %}> {% if multi_qty > 0 %}> > {% endif %}

The above throws an error “Liquid error: comparison of String with 0 failed”

The below works but is it possible to avoid type casting each time

{% assign multi_qty = multi_qty_str | to_i %}> {% if multi_qty | to_i > 0 %}> > {% endif %}

I’ve tried:

{% assign multi_qty = (multi_qty_str | to_i) %}

And:

{% assign multi_qty = multi_qty_str | to_i + 0%}

but still show the same error

Problem solved:

{% assign multi_qty = multi_qty_str | to_i | abs %}

works

1 Like