Rounding Numbers in Power Automate
If you've ever tried to round numbers in Power Automate, you have probably already run into the issue there is no intuitive way to do so.
Rounding Numbers in Power Automate Cloud Flows
Recently, we had a client that needed to calculate price increases based on a Product’s current price in a flow and then round the result. If you have ever spent much time working in Excel, you know there are multiple functions provided for rounding numbers. In fact, if you search "round" in the function box, you will find a list of 15 different functions:
Unfortunately, this function is not as simple in a Power Automate cloud flow, and you won't find any results by searching it.
The Basics of Rounding Numbers in Power Automate Cloud Flows
Thankfully, there is an expression function that lets you round numbers, you just won’t find it under the Math functions. Using the formatNumber String function, you pass in a decimal number as well as a numeric format string, and it will format it the way you specify. Using the format string of “#0”, you can have it return a string of the rounded number, using conventional rounding rules (round down below .5 and up if the number is .5 or greater).
Let’s take a closer look.
For our example, we've created a cloud flow triggered manually with parallel branches. In each branch, we’ve initialized a variable of type float (so that we can include decimal numbers in the example), set them to 842.6 and 842.4, and named them varRoundUp and varRoundDown. This will allow you to round both ways.
After that, we initiate another variable in each branch to enter the expression we'll use to show the output:
Once we save the flow, we can run it and expand the variables. You can see that 842.6 was rounded up to 843 and 842.4 was rounded down to 842.
Keep in mind that the formatNumber function is a string function. This means the value returned is a string and not a number, so if you plan to insert this into a number field or manipulate it further, you need to convert it to a number first.
When you do that, you can then easily add to your expression:
Rounding Whole Numbers in Power Automate Cloud Flows
Now you know how to do simple rounding in a flow, but what if you need to round to the nearest multiple? In our case, the client actually needed the result to be rounded to the nearest $5 instead of the nearest dollar. This requires more work in creating the expression inflow.
Keeping in mind that flow evaluates and executes expressions from the inside to the outside, the first thing we need to do inside of our formatNumber function is divide our number by the multiplier we want, in this example case, 5:
Then we need to convert it to a decimal number:
Then we multiply the result again by our multiplier. Once that is done, your expression should look like this:
If we change our variables to a type of float, replace our function from the first part of this post on each branch, and change the variable name in the expression then run the flow it should look like this:
You can see that the numbers are rounded to the nearest multiple of 5:
Stoneridge Software has a team of experts that can help you navigate this solution or any others that will benefit your business. Please reach out to us so we can help optimize your experience.