Liquid: find number in a string and modify it.

Highlighted
Tourist
6 0 1

I have a Liquid question.  I have a string which could be like "8 lb / Adult" or "3.5 oz".  I want to be able to take the number part, multiply it by some number and then output the result. So for example start with "8 lb / Adult", get the 8, multiply by 2, and put it back in the string to end up with "16 lb / Adult". I'm not sure how to do this or if it's possible in liquid.

0 Likes
Highlighted
Tourist
6 0 1

I figured it out with JS instead. If you're wondering why I needed to do this, the client requested it.

<span id="item-variant-{{ itemCount }}"></span>
<script>
var variantQty = {{ item.quantity }};
var variantTitle = '{{ item.variant.title }}';
var variantTitleNumber = parseFloat(variantTitle);
var variantTitleText = variantTitle.replace(variantTitleNumber,'');
var variantTotal = variantTitleNumber * variantQty;
$('#item-variant-{{ itemCount }}').text( variantTotal + ' ' + variantTitleText );
</script>

 

0 Likes