Liquid Tip: Converting a String to a Number

Liquid Tip: Converting a String to a Number

Did you know that you can convert a string custom attribute in Braze to a number via Liquid when you are composing a message? Simply add a math filter to your string when assigning it.

{% assign ABC = {{custom_attribute.${ABC}}} | plus: 0 %}        

Converting a custom attribute to a number is important any time you need to use that value in a math comparison, such as checking if the value is greater or less than another number.

You might also be storing numbers AND other values in the same attribute. Such as "No" or "N/A". In that case you wouldn't want to attempt to convert those values to a number. You might think that you'd just check the attribute to see if it contains either of those values, but there's always the possibility that the data could be messy and you are getting variations such as "NO" or "n/A" etc. The best solution for this is to check the field to see if it contains any numbers. We can do this by using a for loop to check against a list of numbers.

{% assign number = "0,1,2,3,4,5,6,7,8,9" | split: ',' %}

{% for i in number %}
{% if {{custom_attribute.${ABC}}} contains i %}
{% assign ABC = {{custom_attribute.${ABC}}} | plus: 0 %}
{% break %}
{% else %}
{% assign ABC = {{custom_attribute.${ABC}}} %}
{% endif %}
{% endfor %}        

You could also alter this code to check for only letters, or only certain numbers, etc.


要查看或添加评论,请登录

Shan Parker ?的更多文章

  • Liquid Basics

    Liquid Basics

    Thinking about making the jump to Braze but you're scared of learning a new coding language? I promise it's easier than…

    2 条评论

社区洞察

其他会员也浏览了