The int Function does something super simple. Convert a string into an integer value. That’s it. But, as always, we need to be careful while using conversion functions, so let’s explore it a little bit more because there are some potential hidden issues that you may find.
Usage
It follows a simple pattern.
- String
Example:
int('12')
you'll get:
12
But what happens if it’s a Float?
int('12.5')
Will it return an error or do something else?
Power Automate will return an error message, so you should be careful when passing an argument to an int Function. I have an article explaining how to do it. The same happens to any argument that is not a potential int value, so you can protect your Power Automate and know to fail gracefully if the value is not what you expect.
So far, so good. Let’s explore some edge cases.
Limitations
Be aware that the “int” type has a limitation to the number of digits it can hold. If you try to convert:
int('123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890')
You’ll get:
See my recommendations, but this is an excellent example to protect your Power Automate against invalid values. You can have a string that only contains digits and still have issues. Be aware of this behavior since, as you can see, the error will not help you.
So what’s the limit? You can do a simple Power Automate that can test it for you:
The Power Automate fails after 20 digits, but here’s what we get with 19 numbers:
So even if your Power Automate doesn’t fail, you get an invalid conversion. It’s not what you want at all. You get a rounding and zeros instead of the digits that you defined. Strange, I know, but it’s the expected behavior because the “int” type can only hold 16 numbers, but we would expect a different action, right? Failing after 16 and not allow three more digits while rounding them and adding zeros.
Finally, let’s test with negative numbers:
Exactly the same behavior, so at least you have some consistency on what to expect.
Recommendations:
- Always have a parallel action if you’re converting a value to deal with issues resulting from the conversion. If you don’t know how to do it, read my article explaining the reasoning and how to do it.
- Don’t nest it. You can, and it’s quite a compact function, but it’s always nice to have a step in the Flow that shows you the converted value. It’s especially important if you’re debugging a Power Automate that is failing. The error messages won’t help you, and, as you can see with the previous examples, there are some hidden errors in conversion that may escape and return you invalid results.
- Check the length of the string. If it’s longer than 16 characters, be aware that the number resulting won’t be what you want, and it will break after 19 digits. It’s safer to either exit with an error so that you know that a number is longer than expected or, if you’re confident that you can do it, truncate the string to 16 characters.
I know that you can’t get simpler than a conversion from a string to an int. Still, as you can see, there are some edge cases to take into consideration where Power Automate doesn’t fail but returns unexpected values that can generate invalid data.
Sources:
Microsoft’s int Function Reference
Back to the Power Automate Function Reference.
Photo by Wolfgang Rottmann on Unsplash
Hi, thank you for the great input.
I have this expression
addDays(variables(‘varVMComplete’),int(variables(‘varVMDuration’)),’MM/dd/yyyy’)
How do I get to subtract int(variables(‘varVMDuration’) from variables(‘varVMComplete’) instead of adding (in other terms: how do I convert the second variable to a negative integer)?
Thank you!
Hi Salv,
You can replace it with:
mul(variables(‘varVMComplete’),-1)
This way, you can have a negative number.
Cheers,
Manuel
Hi Maunel,
I enjoyed your article, but wante to say that the maximum value is not linked ot the number of digits but to the recognised maximum of the integer type as defined by Microsoft as 2,147,483,647. This will happily return a positive response from IsInt(), but 2,147,483,648 will fail.
Regards,
Paul