Brian Coughter
asked on
Division by Zero error
When I set this line:
<cfset CM_aVisits = NumberFormat(CM_Visits.Cou nt / CM_uVisits.Count,'999,999, 999,999.9' )>
I get a division by zero error if the count is zero. So then I set this:
<cfoutput>#CM_Avg()#</cfou tput>
<cffunction name="CM_Avg">
<cfif CM_Visits.Count NEQ 0>
<cfset CM_aVisits = 0>
<cfelse>
<cfset CM_aVisits = NumberFormat(CM_Visits.Cou nt / CM_uVisits.Count,'999,999, 999,999.9' )>
</cfif>
<cfoutput>#CM_Visits#</cfo utput>
</cffunction>
However, I still get the error from the 5th line in the function. Is there another way to write this so that CF doesn't throw an error?
<cfset CM_aVisits = NumberFormat(CM_Visits.Cou
I get a division by zero error if the count is zero. So then I set this:
<cfoutput>#CM_Avg()#</cfou
<cffunction name="CM_Avg">
<cfif CM_Visits.Count NEQ 0>
<cfset CM_aVisits = 0>
<cfelse>
<cfset CM_aVisits = NumberFormat(CM_Visits.Cou
</cfif>
<cfoutput>#CM_Visits#</cfo
</cffunction>
However, I still get the error from the 5th line in the function. Is there another way to write this so that CF doesn't throw an error?
ASKER
Okay. I made the change but CF still throws an error on that line in the IF statement.
<cfset CM_aVisits = NumberFormat(CM_Visits.Cou nt / CM_uVisits.Count,'999,999, 999,999.9' )>
It means the value of CM_uVisits.Count is 0
you can't divide CM_Visits.Count by 0
I would do a <cftry> around the value that could be 0
<cffunction name="CM_Avg">
<cfif CM_uVisits.Count NEQ 0> <!--- This line should contain your denominator --->
<cfset CM_aVisits = 0>
<cfelse>
<cftry>
<cfset CM_aVisits = NumberFormat(CM_Visits.Cou nt / CM_uVisits.Count,'999,999, 999,999.9' )>
<cfcatch><cfset CM_aVisits = 0><!--- or whatever value is appropiate ---></cfcatch>
<cftry>
</cfif>
<cfoutput>#CM_Visits#</cfo utput>
</cffunction>
It means the value of CM_uVisits.Count is 0
you can't divide CM_Visits.Count by 0
I would do a <cftry> around the value that could be 0
<cffunction name="CM_Avg">
<cfif CM_uVisits.Count NEQ 0> <!--- This line should contain your denominator --->
<cfset CM_aVisits = 0>
<cfelse>
<cftry>
<cfset CM_aVisits = NumberFormat(CM_Visits.Cou
<cfcatch><cfset CM_aVisits = 0><!--- or whatever value is appropiate ---></cfcatch>
<cftry>
</cfif>
<cfoutput>#CM_Visits#</cfo
</cffunction>
Just caught another error in your if. You should evaluate it on the value equalling 0 as you want it to run the first set statemant if the CM_uVisits.Count variable is 0:
<cffunction name="CM_Avg">
<cfif CM_uVisits.Count EQ 0> <!--- This line should contain your denominator and should check to see if it is equal to 0--->
<cfset CM_aVisits = 0>
<cfelse>
<cfset CM_aVisits = NumberFormat(CM_Visits.Cou nt / CM_uVisits.Count,'999,999, 999,999.9' )>
</cfif>
<cfoutput>#CM_Visits#</cfo utput>
</cffunction>
<cffunction name="CM_Avg">
<cfif CM_uVisits.Count EQ 0> <!--- This line should contain your denominator and should check to see if it is equal to 0--->
<cfset CM_aVisits = 0>
<cfelse>
<cfset CM_aVisits = NumberFormat(CM_Visits.Cou
</cfif>
<cfoutput>#CM_Visits#</cfo
</cffunction>
In addition to shooksm's observation above (EQ instead of NEQ) you were outputting CM_Visist, should be CM_aVisits. This should work fine...
<cfif CM_uVisits.Count EQ 0>
<cfset CM_aVisits = 0>
<cfelse>
<cfset CM_aVisits = NumberFormat(CM_Visits.Cou nt / CM_uVisits.Count,'999,999, 999,999.9' )>
</cfif>
<cfoutput>#CM_aVisits#</cf output>
<cfif CM_uVisits.Count EQ 0>
<cfset CM_aVisits = 0>
<cfelse>
<cfset CM_aVisits = NumberFormat(CM_Visits.Cou
</cfif>
<cfoutput>#CM_aVisits#</cf
could u paste what the cf error was, otherwise i feel danrosenthal has pointed out the proper stuff.
if it doesn't work just paste the error that cf is showing... that would be morehelpfull
i hope CM_Visits.Count is a structure??
Regards
Hart
if it doesn't work just paste the error that cf is showing... that would be morehelpfull
i hope CM_Visits.Count is a structure??
Regards
Hart
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all for your help.
Hart: Special thanks!!! You nailed it!!!
PE_CEF_DEV: I like your thinking on the <cfreturn> !!!
Hart: Special thanks!!! You nailed it!!!
PE_CEF_DEV: I like your thinking on the <cfreturn> !!!
cool :-)
Regards
Hart
Regards
Hart
<cffunction name="CM_Avg">
<cfif CM_uVisits.Count NEQ 0> <!--- This line should contain your denominator --->
<cfset CM_aVisits = 0>
<cfelse>
<cfset CM_aVisits = NumberFormat(CM_Visits.Cou
</cfif>
<cfoutput>#CM_Visits#</cfo
</cffunction>