One of the weakest areas for Power Automate’s functions is in the math and numbers area. For this week’s #FunctionFriday, I will delve into the first batch of math-related functions, I’m going to cover the basic 4: add (addition), sub (subtraction), mul (multiplication), and div (division).
The pattern is basically the same for all of them:
add(5.5, 6) //returns 11.5
sub(5.5, .5) //returns 5.0
mul(3, 3) //returns 9
div(9, 3) //returns 3
You have the function name and then two number parameters. And therein lies the most frustrating part of the functions. You can only pass two parameters. If you need to do math with more than two numbers, you have to call the function multiple times. Like so:
add(5, add(3, add(6, add(7, 2)))) //returns 23
sub(20, sub(10, sub(5, 2))) //returns 13
mul(3, mul(3, 3)) //returns 27
div(27, div(9, 3)) //returns 9
Embedding the functions in this manner can get extremely tricky with the sub and div functions, as the order of operations can easily change your result.
This can also get extremely tedious to work with. For example, if you have an array of numbers of unknown size, you have to implement a loop to process the numbers one pair at a time.
But keeping that in mind, using these functions is pretty straightforward.
Gotchas
There are, however, a couple of gotchas to keep in mind. First, the inputs affect the output. What that means is that if you pass in two integers, the result will be an integer. For example:
div(22, 7) //returns 3
div(19, 2) //returns 9
But if either or both numbers are a float type, the result will be a float data type.
div(22.0, 7) //returns 3.142857142857143
div(19, 2.0) //returns 9.5
And if you store a float result into an integer variable, the result will be an integer.
And, of course, math being math, you can’t divide by 0. Go fig.
There’s also a limit to the size of the numbers. I haven’t been able to find any documentation on exact sizes in the official docs. I did find one blog post that stated that it’s 19 digits total and only 8 digits to the right of the decimal place. But as you can see in my above example, that isn’t accurate as my test output is 15 digits to the right of the decimal point. Another post stated that because it’s based on JavaScript, it has the same limits as JS.
I don’t know for sure what the correct answer is, but if someone can point me at an official doc that has the info, I’d appreciate it.
Summary
That covers the first group of math functions, the basics. I still don’t understand why Power Automate doesn’t support passing in more than 2 arguments, or at being able to pass in an array of numbers. It seems like it would be simple to implement and it’s a sorely lacking area in PA. Hopefully, we’ll see it improve at some point.
The post Function Friday – Add, Sub, Mul, Div first appeared on Barret Codes.
Top comments (0)