How To Convert Floating Point Number to Nearest Integer & also Round Up || Down By Power Automate

 How To Convert Floating Point Number to Nearest Integer & also Round Up || Down By Power Automate



How to Round (Up; Down) Decimal in Power Automate

Decimal numeric Round-off is a renowned Math operation, that will always be a necessity while working with numbers irrespective of their complexity.

When it comes to the Canvas app, we have multiple built-in methods available for Rounding a decimal namely RoundRoundDown and RoundUp.

Round(NumberDecimalPlaces)
RoundDown(NumberDecimalPlaces)
RoundUp(NumberDecimalPlaces)

We also have scenarios to round off the decimal values in Power Automate. But, to our surprise, Microsoft has no specific function provided in the expressions, instead, we will have to use an action named “Format Number” under the “Number Functions” category.

Format number only does a Round function in a traditional way by rounding up if the decimal digit is 5 or higher. Otherwise, this function rounds down. For Instance: 
1.1 to 1, 
1.5 to 2. 



This Format Number action enables us to perform a variety of number formatting options easily by leveraging number formatting patterns like other Power Platform services. Formatting a number includes a custom format, which is used to Round up a decimal value to an integer just by specifying “#” in the format field. This action also can

 

  • Format the appearance of the number based on typical patterns such as Currency($) formats.
  • Custom formatting patterns likely to Excel, Power BI and Power Apps expressions.
  • Try currency formats as per the business requirements, specific to locales.
  • Also formats C2 (currency with two decimal places), and N2 (negative number with two decimal places).
For a customized Round up option like 
1.1 -> 2; 

1.5 -> 2, a manual arithmetic expression should be built within the variable action. 



Round Up for Single Digits 👇
    <Expression> if(and(less(int(last(split(string(variables('varIntDecimal')),'.'))),int(9)), greaterOrEquals(int(last(split(string(variables('varIntDecimal')),'.'))),int(1))), add(int(first(split(string(variables('varIntDecimal')),'.'))),1), int(first(split(string(variables('varIntDecimal')),'.'))))


Round Up for Double Digits 👇

if(and(lessOrEquals(int(last(split(string(variables('varIntDecimal')),'.'))),int(99)), 
greater(int(last(split(string(variables('varIntDecimal')),'.'))),int(00))),
add(int(first(split(string(variables('varIntDecimal')),'.'))),1), int(first(split(string(variables('varIntDecimal')),'.'))))



ROUND DOWN EXPRESSION 👇

sub(outputs('Round_Up_for_Double_Digits'),1)


FLOW DOWNLOAD LINK - Convert_Floating_Point_Number_Integer_Round_UP_Down_20231031174205.zip


code explain-

The expression you've provided is a Power Automate expression, often used in Microsoft Power Automate (formerly known as Microsoft Flow) to perform conditional logic and data manipulation. Let's break down this expression step by step:

int(last(split(string(variables('varIntDecimal')),'.')):

variables('varIntDecimal'): This refers to a variable in your flow, and it seems to contain a decimal number.
string(...): Converts the variable into a string.
split(..., '.'): Splits the string into an array of substrings using the period (dot) as the delimiter.
last(...): Returns the last item in the array.
int(...): Converts the last item back into an integer.
This part of the expression extracts the integer value after the decimal point in varIntDecimal.

int(99) and int(00):

These are simple integer constants - 99 and 0, respectively.

lessOrEquals(..., ...):

Checks if the first value (integer extracted from varIntDecimal) is less than or equal to the second value (99).

greater(..., ...):

Checks if the first value (integer extracted from varIntDecimal) is greater than the second value (0).

if(..., ..., ...):

This is a conditional statement that has three parts:

The first part is the condition that evaluates to either true or false based on the comparisons made earlier.
The second part is the value to return if the condition is true.
The third part is the value to return if the condition is false.
In this case, if the integer extracted from varIntDecimal is less than or equal to 99 and greater than 0, the expression returns the following:

add(int(first(split(string(variables('varIntDecimal')),'.')), 1): This part adds 1 to the integer value before the decimal point in varIntDecimal. It does so by:

Converting varIntDecimal to a string.
Splitting the string at the decimal point.
Taking the first part (before the decimal point) and converting it to an integer.
Adding 1 to the integer value.
int(first(split(string(variables('varIntDecimal')),'.')): If the condition is false, it simply returns the integer value before the decimal point without any modifications.

In summary, this Power Automate expression takes a variable varIntDecimal, which presumably contains a decimal number, and based on the conditions, it either increments the integer part of the decimal by 1 or returns the integer part as it is. It's a way of performing conditional data manipulation in your flow.

Thanks for reading!

I hope you found this blog on the Microsoft Power Platform helpful! From Power Apps, Power Automate (Cloud & Desktop), Canvas Apps, Model-driven Apps, Power BI, Power Pages, SharePoint, Dynamics 365 (D365), Azure, and more, I cover a wide range of topics to help you harness these powerful tools. Don’t miss out on future tips, tutorials, and insights—hit that subscribe button to get the latest posts right to your inbox. 💌

💬 I’d love to hear your thoughts! Drop a comment below with your questions, ideas, or feedback—let’s get the conversation started!

🔗 Let’s connect and grow together!
Follow me, Ravindra Jadhav, on your favorite platforms for even more content and updates on Microsoft Power Platform and related technologies:

  • 💼 LinkedIn – Let’s network and share ideas!
  • 💻 GitHub – Explore my projects and code.
  • 🐦 Twitter – Stay updated with quick tips and industry news.
  • 📺 YouTube – Watch tutorials and deep dives on Power Platform, Power Apps, Power Automate, and more!

Let’s build something amazing together with Power Platform and Azure! 🚀




Post a Comment

0 Comments