In this section from my Power Automate I initialise two variables (currentscore and maxscore) with values using the Set variable action. I want a third variable, sspercentage, to be equal to:
(currentscore) / (maxscore) * 100
that is, a percentage but with only two decimal places. To do that I need to use the following formula in the Set variable 3 action:
formatNumber(mul(div(variables(‘currentscore’),variables(‘maxscore’)),100),’0.00′,’en-US’)
which you will note, if you look carefully, actually results in sspercentage being a string not a float!
(div(variables(‘currentscore’),variables(‘maxscore’))
handles the division
mul(div(variables(‘currentscore’),variables(‘maxscore’)),100)
multiples the division by 100 to get the percentage value.
The 0.00 in the formula is the actual formatting that you can adjust if you need and the en-US is the language format for the number. The Microsoft reference is for the formatnumber function is here:
The end result looks like:
If the formatnumber function is not used then the result contains a lot of numbers after the decimal place and remains of type float, which I would only need if I wanted to display the value of Pi!
Hey Robert, I cant this to work. It all makes sense but when im doing it, I still w
I have a “set variable” that is a string, called Percentage. The function I am using in it is:
Ive also tried creating “Compose” for each of the steps, so a compose for the outputs of the two variables, and a compose of the Div, Mul, format number, a set variable (string) for each also, just to see where it falls over. Its getting the right numbers in the variables Disk Size aand Free Space, but after that, its just 0’s.
If you rather, you can send me a Teams, but i thought I would post here in case the solutio, if you can help me find one, helps others!
Cheers,
Dean E.
LikeLike
I should have spell checked that before posting… 😬
LikeLike
To display a percentage with two decimal places in Power Automate, you can use either the **Format Number action** or the **formatNumber function**. Here’s how to do both:
### Method 1: Using the Format Number Action
1. **Add a Trigger**:
– Start by creating a new flow and adding a trigger (e.g., “Manually trigger a flow”).
2. **Add a Number Input**:
– Add a **Number input** to your flow to capture the number you want to convert to a percentage.
3. **Add Format Number Action**:
– Add the **Format Number** action to your flow.
– In the **Number** field, select the dynamic content that corresponds to your number input.
– In the **Format** field, enter `P2` to format it as a percentage with two decimal places.
– Optionally, specify a locale (e.g., `en-US`).
4. **Example Configuration**:
– **Number**: `triggerBody()[‘number’]`
– **Format**: `P2`
– **Locale**: `en-US`
5. **Run the Flow**:
– Save and run the flow, entering a number (e.g., `0.1234`). The output will display as `12.34 %`.
### Method 2: Using the formatNumber Function
1. **Add a Trigger**:
– Create a new flow and add your desired trigger.
2. **Initialize Variables**:
– Initialize two variables for your current score and maximum score (if applicable).
3. **Set Variable with formatNumber Function**:
– Add a **Set variable** action where you want to calculate and format the percentage.
– Use the following expression in the value field:
“`plaintext
formatNumber(mul(div(variables(‘currentscore’), variables(‘maxscore’)), 100), ‘0.00’, ‘en-US’)
“`
– This expression will calculate the percentage of `currentscore` relative to `maxscore`, multiply by 100, and format it to two decimal places.
4. **Example Configuration**:
– If `currentscore` is 75 and `maxscore` is 100, this will yield `75.00`.
5. **Run the Flow**:
– Save and run your flow to see the formatted percentage output.
### Summary of Expressions
– Using **Format Number Action**:
“`plaintext
P2
“`
– Using **formatNumber Function**:
“`plaintext
formatNumber(mul(div(variables(‘currentscore’), variables(‘maxscore’)), 100), ‘0.00’, ‘en-US’)
“`
### Conclusion
By following these methods, you can effectively display percentages with two decimal places in Power Automate, ensuring that your data is presented clearly and professionally in any outputs or notifications generated by your flows.
Citations:
[1] https://www.spguides.com/format-number-to-percentage-in-power-automate/
[2] https://tomriha.com/how-to-format-numbers-currency-percentage-in-power-automate/
[3] https://www.youtube.com/watch?v=AJEITs2NpDc
[4] https://blog.ciaops.com/2022/05/19/displaying-a-percentage-with-two-decimal-places-in-power-automate/
[5] https://stackoverflow.com/questions/68310896/display-a-whole-number-or-decimal-field-as-a-percent-in-a-powerapps-model-driven
[6] https://sharepoint.stackexchange.com/questions/304671/format-number-power-automate
[7] https://jgvjg48436.lithium.com/t5/General-Power-Automate/Number-format-to-change-decimal-to-percentage/td-p/2629700
LikeLike