Hi All!
I have a float that I need to round to the nearest whole number. For example: 3.480987 will be "3" while 3.7685 should be "4". I can't seem to find a round function. Is there? What are some work-arounds for this if there isn't any... thank you!
-bochie
All the answers here were very useful to find the best fitting solution for me.
Let's start with my final solution and then work towards the requested solution of Bochie.
I am using the following expression to round a float as string to a decimal with one digit behind the decimal divider:
formatNumber(add(float(triggerOutputs()?['body/score']),0.00000000000001),'F1')
(background is, that a SharePoint calculated column is handed over to Power Automate as a string, and even if you set it in SharePoint to have one decimal digit, it will always hand over the full float)
This formula is working fine for me, but now let me make it a bit easier for you:
First, let's assume, the variable <floatVar> is already a float, so we remove the SharePoint calculated field 'score' and replace it with a placeholder, that you will have to replace in your expression:
formatNumber(add(<floatVar>,0.00000000000001),'F1')
and now, let's look at the rule for rounding up or off when next digit is a 5*:
for me, the requirement was, that a number 3.25 should be rounded up to 3.3, but the default for formatNumber is, that it will round off in that case.
Therefore, I have to add the smallest possible number, so that it gets a bit more than exactly 3.25 and so it rounds up.
But if you want to use the default behaviour to have 3.2 in case of 3.25, the formula is even easier:
formatNumber(<floatVar>),'F1')
And now, to finally answer Bochie's question, the easy round function for float/decimal numbers to whole numbers is just to set the number of decimal digits to 0:
formatNumber(<floatVar>),'F0')
Little bonus:
In all cases, formatNumber will only return a string, even if it looks like a number, so if you intend to do calculations, you need to again convert the resulting string to an int or float like in the following example! 😉
int(formatNumber(<floatVar>,'F0'))
With all these examples and tweaks, I hope everybody is now able to find the ideal solution for your exact use case.
See also this site for a definition of formatNumber expression:
For example, if I have a number like 7.978 or 7.890, I would like it to be rounded up to 8.0. However, if the number is 7.776 or 7.678 , I want it to be displayed as 7.7 or 7.6. How can I do this?
Considering that the goal is to round to the nearest integer ("whole number"), this would do the work
@{if(greater(sub(float(myNumber), int(formatNumber(float(myNumber), 'F0'))), 0.5), add(int(formatNumber(float(myNumber), 'F0')), 1), int(formatNumber(float(myNumber), 'F0')))}
or
@{if(greaterOrEquals(sub(float(myNumber), int(formatNumber(float(myNumber), 'F0'))), 0.5), add(int(formatNumber(float(myNumber), 'F0')), 1), int(formatNumber(float(myNumber), 'F0')))}
Let's see a few examples:
Example 1:
Using two actions: Compose 3 and Compose 4.
In Compose 3, one uses as input the number 3.480987.
In Compose 4, one uses as input the expression
@{if(greater(sub(float(outputs('Compose_3')), int(formatNumber(float(outputs('Compose_3')), 'F0'))), 0.5), add(int(formatNumber(float(outputs('Compose_3')), 'F0')), 1), int(formatNumber(float(outputs('Compose_3')), 'F0')))}
This gives the following output:
Example 2
Using two actions: Initialize variable and Compose 5.
In Initialize variable one uses a float 3.7685 with the name e myNumber.
In Compose 5, uses as input the expression
@{if(greater(sub(float(variables('myNumber')), int(formatNumber(float(variables('myNumber')), 'F0'))), 0.5), add(int(formatNumber(float(variables('myNumber')), 'F0')), 1), int(formatNumber(float(variables('myNumber')), 'F0')))}
This gives the following output
---
Example 3
Now let us test the second case, with greaterOrEquals.
For that, one will use two actions: Compose and Compose 2
This is the output
---
Notes:
• In Power Automate, expressions need to be enclosed within the @{} syntax when added in the "Inputs" field of actions.
• Relevant read: Use expressions in conditions to check multiple values.
• If all one wants it to round up a number, one can do the following:
@{add(int(formatNumber(Value,'F0')),1)}
Thank you for answering this one. I was looking for the rounding option in Power Automate Flow as well. It's still not available in Flow. I hope this is coming soon.
Untill then, great solution! I got an error that it was a string. To handle this, I put the whole calculation into a Float(). Works as a charm now 🙂
One small change - I probably should have used GreaterOrEquals() rather than greater()
I wish there were a round() expression....
Please see my example below - I don't see a way to make is less complicated but I wish I could.
Here is the expression that I used:
WarrenBelz
146,651
Most Valuable Professional
RandyHayes
76,287
Super User 2024 Season 1
Pstork1
65,999
Most Valuable Professional