Skip to main content
Post

#PowerPlatformTip 155: 'Float Before Div for Correct Decimals in Power Automate'

#PowerPlatformTip 155: 'Float Before Div for Correct Decimals in Power Automate'

TL;DR: In Power Automate div() truncates when both operands are integers. Cast them with float(), div(float(a), float(b)), so the result is a Float and your decimals survive.

You build a percentage or ratio from two whole numbers and the result is 0 or a rounded-down integer instead of a proper decimal. The reason is simple: div() returns an Integer whenever both operands are integers, it truncates, it does not round. Convert the operands to Float first and everything lines up.

💡 Challenge

You want a completion or share percentage, for example matching records versus all records. A natural expression looks like this:

1
mul(div(outputs('Count_Matching'), outputs('Count_All')), 100)

With Count_Matching = 75 and Count_All = 200 you expect 37.5, but you get 0. Both outputs are integers, so div(75, 200) truncates to 0 and mul(0, 100) stays 0. Whenever the dividend is smaller than the divisor the result collapses to zero, and larger values silently lose their decimals.

✅ Solution

div() only returns a Float when the dividend or the divisor is a Float. Wrap both operands in float() so the division keeps its decimal places:

1
mul(div(float(outputs('Count_Matching')), float(outputs('Count_All'))), 100)

With the same inputs this now returns 37.5. One Float operand is technically enough, but casting both is explicit and safe when the source type can vary.

🔧 How It’s Done

1. Spot the integer division.

🔸 Look for div() where both operands are whole numbers, for example Compose outputs, length() results, counts or integer variables.

🔸 Quick test in the expression editor: div(11, 5) returns 2 (not 2.2) and div(75, 200) returns 0.

2. Cast both operands to Float.

🔸 Change div(a, b) to div(float(a), float(b)):

1
mul(div(float(outputs('Count_Matching')), float(outputs('Count_All'))), 100)

🔸 As soon as one operand is a Float, div() returns a Float, e.g. div(float(75), float(200)) returns 0.375.

Power Automate real flow run: div(7, 2) with two integer operands truncates to 3 Real flow run: @div(7, 2) with two plain integer operands truncates to 3, the .5 is simply gone.

Power Automate real flow run: div(float(7), float(2)) returns the correct 3.5 Real flow run, same numbers: @div(float(7), float(2)) correctly returns 3.5, casting both operands to Float is the entire fix.

3. Format the output for display (optional).

🔸 Wrap the result in formatNumber(..., 'N2') for a clean two-decimal value:

1
formatNumber(mul(div(float(outputs('Count_Matching')), float(outputs('Count_All'))), 100), 'N2')

🔸 This returns 37.50 as a string. If you keep calculating on it, parse it back with float(formatNumber(...)).

4. Guard against divide-by-zero.

🔸 div() fails when the divisor is 0. Protect the expression with if():

1
if(equals(outputs('Count_All'), 0), 0, mul(div(float(outputs('Count_Matching')), float(outputs('Count_All'))), 100))

🔸 A zero total now returns 0 instead of throwing an error.

🔄 The Other Way Round: Float to Integer

The reverse question comes up just as often: you have a Float and need a whole number. Every result below is from one flow run.

1. int() does not truncate, it rejects decimals.

🔸 int('42') returns 42, but int(3.7), int(-3.7) and int('3.7') all fail with The template language function 'int' was invoked with a parameter that is not valid. The value cannot be converted to the target type.

🔸 int() is a type conversion for values that already are whole numbers, not a rounding function.

2. Truncate (cut the decimals off).

🔸 Integer div() truncates towards zero: div(7, 2) returns 3, div(-7, 2) returns -3.

🔸 For an existing Float, cut at the decimal point: int(first(split(string(-3.7), '.'))) returns -3.

🔸 The fractional part alone: mod(7.5, 1) returns 0.5.

3. Round to the nearest whole number.

🔸 formatNumber() rounds, then int() converts the string back:

1
int(formatNumber(variables('value'), '0'))

🔸 int(formatNumber(3.7, '0')) returns 4.

🔸 Halves round to the even neighbour (banker’s rounding): formatNumber(3.5, 'N0') returns "4", formatNumber(2.5, 'N0') returns "2", formatNumber(3.49, 'N0') returns "3".

🔸 Use '0' or 'F0' rather than 'N0' for values above 999: formatNumber(1234.5, 'N0') returns "1,234" with a thousands separator, formatNumber(1234.5, 'F0') returns "1234". In this run int('1,234') still gave 1234, but the separator-free format is the one to rely on.

4. float() and the decimal comma.

🔸 float('1.5') returns 1.5, and float('1,234.5') returns 1234.5: the comma is read as a thousands separator.

🔸 That is why float('1,5') returns 15, not 1.5. No error, the comma is simply dropped. Text from German, Swiss or French sources needs the comma swapped first: float(replace(variables('text'), ',', '.')) returns 1.5.

🎉 Result

Your percentage and ratio calculations return accurate decimals instead of 0 or truncated integers, no Azure Function and no extra actions, just the built-in float() conversion, optionally formatted with formatNumber().

🌟 Key Advantages

🔸 Correct math: decimals survive instead of being truncated to Integer.

🔸 No workaround actions: a pure expression fix, no extra Compose or Scope needed.

🔸 Locale-safe display: formatNumber(..., 'N2') gives predictable, formatted output.

🔸 Robust: combined with an if() guard it also handles divide-by-zero cleanly.

🛠️ FAQ

1. Why does div() drop my decimals?

If both operands are integers, div() returns an Integer and truncates. It only returns a Float when the dividend or the divisor is a Float, so div(11, 5) is 2 but div(11.0, 5) is 2.2.

2. Do I have to cast both values, or is one enough?

One Float operand is technically enough to force a Float result. Casting both with float() is more explicit and protects you when the input type can change.

3. Why is my result a string after formatting?

formatNumber() returns a string. If you need to keep doing math, parse it back with float(formatNumber(...)).

4. How do I avoid a flow failure when the divisor is 0?

Wrap the expression in if(equals(divisor, 0), 0, div(...)) so a zero total returns 0 instead of throwing an error.

5. How do I convert a float to an integer in Power Automate?

int(3.7) fails, int() only accepts whole numbers. To round use int(formatNumber(value, '0')) (3.7 → 4, halves go to the even number). To truncate use integer division, div(7, 2) → 3, or cut the string at the decimal point: int(first(split(string(value), '.'))).

6. Why does float('1,5') return 15?

float() treats the comma as a thousands separator and drops it. Replace it with a dot first: float(replace(text, ',', '.')) returns 1.5.

This tip is by Marcel Lehmann, Microsoft MVP (Business Applications) and founder of the Power Platform Academy, which runs free live workshops (in German). See the next free workshop dates (in German). For companies that want this implemented for them, see KMUpower: Power Platform consulting & automation for SMEs (DACH).

This post is licensed under CC BY 4.0 by the author.

Microsoft and the Power Platform product names (for example Power Apps, Power Automate, Copilot Studio) are trademarks of the Microsoft group of companies. This is an independent website. It is not operated, sponsored or endorsed by Microsoft.