# Microsoft Flow and Azure Logic Apps – Math and If Function Workarounds

Microsoft Flow is built on Azure Logic Apps and uses the same workflow language functions. However, there are some limitations of the math functions included in Logic Apps that need some creative workarounds, so here’s a few ways to work around issues. I mentioned in my Quick Formula Expression Guide that you can’t use the arithmetic operators you would expect. Instead, you must use functions to perform operations, but there are some quirks.

## Int() Doesn’t Accept Floating Point Numbers

Everyone expects that the int() function should return an integer based on a floating point input. However, if you provide int() with a number that has a decimal component, it will throw an exception. There’s a solution to this. Instead of int(variable) use div(variable, 1). The div() math function returns an integer (not a float as you would expect – more on that in a moment). It’s the number of whole times the number can be divided. So, you can get an integer by dividing your floating point by 1.

## Div() Returns an Integer

There are two kinds of division in math. Floating point division is what we normally expect. This is the kind of division we were taught in elementary school. There’s also a second kind of integer division that returns the whole number of times that the divisor goes into the dividend. The remainder is dropped (but can be fetched with the mod() function). In Logic Apps, the div() returns an integer division – not a floating point one. However, there’s another way to solve the problem of division resulting in a floating point number.

Every division can be accomplished with a multiplication. mul() returns a floating point. In the simple example, if you want to divide by 100, you can also multiply by 0.01. (I.e. div(variable, 100) becomes mul(variable, 0.01) after conversion.) So, you can convert your divisions into a multiplication of a fraction. Factors of 10 are obviously easy, but other numbers can be done as well. Simply use a calculator to divide 1 by the number in question, and you’ll have the factor you need to multiply by. (or enter the number you want to divide by and use the great 1/x key available on some calculators)

## Truncating the Number of Decimals

When dealing with currency, it’s often important to truncate the number of decimal places in the calculation to two decimals. Given that there’s no round() function in Logic Apps, we have to settle for truncation. However, even this becomes a bit “interesting” due to the limitations. Take a look at this sequence:

mul(

div(

mul(variable, 100)

,1)

, 0.01)

It multiplies the variable by 100 to make two decimal places into whole numbers, then uses div() to eliminate the fractional component, and finally uses mul() to shift the number back two decimal places to the right.

## If False, then True

Another quirk that exists in the Logic Apps language is that the true side of an if statement is **always** evaluated. Say we have the if statement:

if(false, div(0,0), 1)

This will throw an exception because the div(0,0) results in a division by zero error. You must convert the if statement so that the true side can always be executed:

if(true, 1, div(0,0))

In this case, the result will be 1 and no exception will be thrown.

While this isn’t the way other languages work – nor is it desirable – it is the way that it works today, and it might explain some odd exceptions your Microsoft Flows are throwing.