Tag Archives: string to integer

string to integer, string to decimal in liquid template–Is typecasting through liquid in PowerApps portals/ Dynamics 365 Portals adding to your woes? the type filter may just be you way out

First of all this is not specific to Dynamics 365/ PowerApps portals. However since I try to put everything related to my passion – dynamics 365 and PowerApps, I try to relate every blog post of mine with that.

Coming to the topic, this is related to liquid. And as the title say’s, it is related to typecasting.

Say you have string variable. And you want it to typecast to integer and perform some sum/ subtraction operations with it.

Let’s take this example.

{% assign num1 = “2” | plus: 10 %}

{% assign num2 = num1 | plus: 20 %}

{{ num2 }}

What do you expect num2 value? You may be expecting 32. But it will print 21020.

If you search the net, you will find workarounds like {% assign num1 = “2” | plus 0 %}. Unfortunately it won’t work as well.

So what is resolution? Liquid type filter comes to the rescue. The below code shall work just fine.

{% assign num1 = “2” | integer | plus: 10 %}

{% assign num2 = num1 | plus: 20 %}

{{ num2 }}

Now num2 will be printed with value 32.

The below type filters are supported

  • boolean
  • decimal
  • string
  • integer

In case the typecasting fails, it is always suggested to use the default filter to specify the default value in case of failure

For example – {{ “blah blah” | boolean | default: false }}. It is obvious that typecasting for this shall fail. So the output would be default value specified which is false.

This is even very useful where you are working with query strings. Query string value will always be in string format. So you would need to typecast them in the type you want.

For example – {% assign increment_value = request.params[‘increment_value’] | integer %}

Hope this helps!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)