September 18, 2020

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.

  1. 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:

  1. 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.
  2. 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.
  3. 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

Manuel Gomes

I'm a Project Manager with experience in large projects and companies. I've worked in the past for companies like Bayer, Sybase (now SAP) and I'm currently working for Pestana Hotel Group.

View all posts by Manuel Gomes →

Leave a Reply

%d bloggers like this: