Do you need to round numbers to 2 decimal places in Power Automate?
Sometimes numbers can get too cumbersome to deal with when they have many digits after the decimal point. Many times two or three decimal places would be a sufficient degree of accuracy for the numbers.
This is where rounding a number to 2 decimal places comes in handy. It can simplify the number while still providing a level of precision that is appropriate for the situation.
Unfortunately, there is no obvious way to get this in Power Automate. There is no rounding action or function available.
But this can still be achieved by formatting the number as a text and then converting it back to a floating point decimal value.
This post is going to show you how to round numbers in your flows.
Using the formatNumber Function to Round Decimal Numbers
The formatNumber function allows you to format numbers in a variety of ways. This function can be used to format numbers with a specific number of decimal places, add currency symbols, and add thousands separators.
Basics of FormatNumber Function
The syntax for the formatNumber function in Power Automate is as follows.
formatNumber(number, format, locale)
number
(required): The number that needs to be formatted.format
(required): The format that should be applied to format the number.locale
(optional): The locale that is used to format the number.
To use the formatNumber function, you need to specify the number
you want to format and the format
you want to apply. The format is specified using a combination of letters and symbols that represent different formatting options.
Using FormatNumber Function for Rounding
The formatNumber function can be used to round numbers to a specific number of decimal places.
To round a number to two decimal places, you can use the format '0.00'
. This format will round the number to two decimal places and ensure there is at least one unit number to the left of the decimal.
formatNumber(3.14159265359,'0.00')
For example, if you have the number 3.14159265359 y
ou can use the formatNumber function to round it to two decimal places by using the above expression.
This will return the rounded number 3.14.
📝 Note: The numberFormat function will round to the nearest value. This means 3.145 would get rounded up to 3.15, and 3.144 would get rounded down to 3.14.
Convert formatNumber Results to Float Data Type
The problem with the formatNumber function is that the result is a string and not a float data type. This means if you try and use the value in further numerical calculations, you will get an error.
If you plan on performing further calculations, you will need to convert the formatNumber results back into a number.
This can be done with the float function. The float function will convert a string into floating point decimal value.
float(formatNumber(outputs('Number'),'0.00'))
For example, the above expression will format the number in the compose action named Number with 2 decimal places, then convert the results to a floating point decimal value.
💡 Tip: This can be used to round your number to any amount of decimal places. For example, you can replace '0.00'
in the formula with '0.000'
to round to 3 decimal places.
Rounding Decimals Up or Down
In this section, we will explore how to round numbers up or down.
This is a much more difficult process since there is no function in Power Automate that can round up or round down.
To achieve this, you will need to round the number first and then check if it was rounded up or down. Then you can return the appropriate number based on this check.
Round Up
if(
greaterOrEquals(
float(formatNumber(outputs('Number'),'0.00')),
outputs('Number')
),
formatNumber(outputs('Number'),'0.00'),
formatNumber(add(outputs('Number'),0.01),'0.00')
)
The above expression will round a number up to two decimal places. Here’s how it works.
float(formatNumber(outputs('Number'),'0.00'))
: This part of the expression takes the numberoutputs('Number')
and formats it to two decimal places using the formatNumber function. This will round the number to the nearest decimal place. The float function then converts this back to a number so it can be compared to the original number.greaterOrEquals(..., outputs('Number'))
: This part of the expression checks if the rounded value from step 1 is greater than or equal to the original input number. If it is, then the number was rounded up.formatNumber(outputs('Number'),'0.00')
: If the condition in step 2 is true, this part of the expression returns the number rounded up to two decimal places.formatNumber(add(outputs('Number'),0.01),'0.00')
: If the condition in step 2 is false (meaning the number was rounded down), this part of the expression adds 0.01 to the input number to effectively round up, then formats the result to two decimal places.
This expression results in a text string, but you can use the float function to convert it to a floating point decimal number if needed.
Round Down
if(
greater(
float(formatNumber(outputs('Number'),'0.00')),
outputs('Number')
),
formatNumber(sub(outputs('Number'),0.01),'0.00'),
formatNumber(outputs('Number'),'0.00')
)
The above expression will round a number down to two decimal places. Here’s how it works.
float(formatNumber(outputs('Number'),'0.00'))
: This part of the expression takes the numberoutputs('Number')
and rounds it to the nearest two decimal places using the formatNumber function. The float function then converts it back to a decimal number data type so it can be compared to the original value.greater(..., outputs('Number'))
: This part of the expression checks if the rounded float value from step 1 is greater than the original input number. If it is, the number was rounded up.formatNumber(sub(outputs('Number'),0.01),'0.00')
: If the condition in step 2 is true, this part of the expression subtracts 0.01 from the number to effectively round down and then formats the result to two decimal places.formatNumber(outputs('Number'),'0.00')
: If the condition in step 2 is false (meaning the number was rounded down), this part of the expression returns the input number formatted to two decimal places.
Again, this expression results in a text string and the float function can be used to convert it to a floating point decimal number if needed.
💡 Tip: These formulas to round up and down can easily be modified to round up or down to any number of decimal places. For example, to round up or down to the 3rd decimal place, replace 0.01
and '0.00'
with 0.001
and '0.000'
respectively in either of the formulas.
Conclusions
Rounding numbers to two decimal places in Power Automate can be accomplished using the formatNumber function to format it to two decimal places.
This creates a string value, but the float function can be used to convert the string back into a number.
When you need to round up or round down, the process is more complex because these functions don’t exist in Power Automate.
A conditional if function can be used to get the numbers rounded up or down to two decimal places.
Have you needed to round numbers in your flow? How did you do it? Let me know in the comments!
0 Comments