Search techniques

  • Yellow belt
  • Standard
  • Tip 155

float() before div() for correct decimals

Quiz question

What does the expression div(75, 200) return?

Solution

  1. A) 0, because both values are whole numbersCorrect. With two whole numbers div() does integer division and cuts off the decimals.
  2. B) 0.375, because div() always returns decimalsWrong. You only get a decimal when at least one value is a decimal, for example after float().
  3. C) An error, because 75 is smaller than 200Wrong. A smaller dividend is not an error. The division would only fail on a divisor of 0.

What it is about

You calculate a share from two counts and get 0 instead of 37.5. The reason: div() does integer division when both values are whole numbers and cuts off the decimals. div(75, 200) therefore returns 0, and times 100 it stays 0. As soon as one of the two values is a decimal, div() returns a decimal. The safest way is to convert both values with float(). For display you round the result to two places with formatNumber(), but that turns it into text. And protect the division with if() when the divisor can be 0, otherwise the action fails.

The expression from the tip

div(float(a), float(b))

When to use it, when not

Use it

For percentages, shares and averages from counts or length().

Better not

When you want a whole number on purpose. Then integer division is correct, div(7, 2) returns 3.

Most common mistake

Calculating further with the result of formatNumber(). It is text and needs float() to become a number again.

License

Standard Runs without a premium connector, so the license from Microsoft 365 is enough. Source on Microsoft Learn: Power Automate licensing FAQ. For your whole flow: License check.

Full tip on powerplatformtip.comPowerPlatformTip #155, with screenshots

Share

Short link: powerautomate.ninja/en/155

Source: PowerPlatformTip #155, Microsoft Learn: Reference for functions in workflow expressions.