Formula trouble using “Integer Divide” (the backslash)

Friday 19 June 2009 @ 10:29 pm

One of my customers sent me a report with an unusual “divide by zero” error. I was going to explain to her how you prevent this error with an IF-THEN statement.  You check to make sure that the field you are dividing by is not zero, like this:

If {FieldA} = 0 then 0 else
{FieldB} / {FieldA}

But when I looked at her formula I noticed that the first line did exactly what I would have done, and it was still generating a divide by zero error on certain values.  But then I noticed something odd about the second line. Her divide sign was a backslash, not the forward slash you see in the example above.  I remembered that a backslash is a special type of divide, but have never used it so I had to look it up in “Help” to see exactly what it did. Even then I had to do some testing because the “Help” explanation wasn’t very clear.

A backslash tells CR to do an “Integer Divide”.  It still does a division, but there are two differences in the calculation:

1) Both the numerator and the denominator are rounded to integers before the division takes place. So:

3.9 \ 1.3 = 4

The numerator and denominator are rounded to 4 and 1 before the division.

2) The end result is truncated to the next lower integer. So:

29 \ 10 = 2

because 2.9 truncates to 2.o.

The problem my customer had was that the denominator was less than .5 and so it rounded to zero.  That caused the divide by zero error.  So if you get this error you have one more thing to check – the direction of your slash. And if anyone can think of a practical application for this type of division, please let me know.  I have never seen one.

(For examples of my most popular formulas, please visit the FORMULAS page on my website.)






One Response to 'Formula trouble using “Integer Divide” (the backslash)'

  1. Mike McCracen - July 22nd, 2009 at 11:21 am

    One practical use of the \ or integer division is to convert an integer Total number of seconds to a more viewable format of hh:mm:ss. It is necessary to do the conversiion if the total may be more than 24 hours.

    Using the \ with an integer divisor never causes a problems and in this case means you don’t have to truncate the result.


Leave a Reply

Jeff-Net
Recrystallize Pro

Crystal Reports Server