Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

"Division by zero is not allowed" error

Participant ,
May 02, 2006 May 02, 2006
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
2.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
May 02, 2006 May 02, 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>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 02, 2006 May 02, 2006
Thanks for the reply, when I run it though it throws; "The value "N/A" cannot be converted to a number "...?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
May 02, 2006 May 02, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 02, 2006 May 02, 2006
LATEST
Thanks Paross, I just realised the number error was due to me using decimalFormat on my output, but i need that so have used your suggestion and it works a treat, many thanks
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources