Skip to main content
Inspiring
May 2, 2006
Question

"Division by zero is not allowed" error

  • May 2, 2006
  • 1 reply
  • 2750 views
Hi all, I am using the syntax below:
<cfset PercentageOfConversionTotalLG = (totalCases.RecordCount/staffMember.RecordCount)*100/>

basically two recordset totals divided to give me a figure, sometimes this figure turns out at 0, so obviouslly 0% and that is what it shows, but other times it shows this error: Division by zero is not allowed, and refers to the above line of code... is there a way around this error? does anyone know please?

Many thanks in advance
    This topic has been closed for replies.

    1 reply

    Participating Frequently
    May 2, 2006
    > is there a way around this error?

    Yeah, don't divide by zero.

    In any programming language, if the denominator of a division operation is zero, an error is thrown. This is proper math.

    Wrap it in a <cfif..>

    <cfif staffMember.RecordCount neq 0>
    <cfset PercentageOfConversionTotalLG = (totalCases.RecordCount/staffMember.RecordCount)*100>
    <cfelse>
    <cfset PercentageOfConversionTotalLG = "N/A">
    </cfif>
    Inspiring
    May 2, 2006
    Thanks for the reply, when I run it though it throws; "The value "N/A" cannot be converted to a number "...?
    Participating Frequently
    May 2, 2006
    Then change N/A to 0 so that PercentageOfConversionTotalLG will be 0%, if that is the result that you would expect if staffMember.RecordCount is zero.

    <cfif staffMember.RecordCount neq 0>
    <cfset PercentageOfConversionTotalLG = (totalCases.RecordCount/staffMember.RecordCount)*100>
    <cfelse>
    <cfset PercentageOfConversionTotalLG = 0>
    </cfif>

    Phil