Solved

Coldfusion/MySql error - Division by zero is not possible

Posted on 2013-06-27
6
513 Views
Last Modified: 2013-06-28
My template has a Coldfusion variable "priceperfoot" that is calculated by dividing the "price" by "totalfinishedsqft" for a home. In the database the totalfinishedsqft field can be null, 0, or a number greater than 0.  If a record has null or 0 totalfinishedsqft then I get the error Division by zero is not possible, if it has a number greater than 0, it works fine. Can someone look at my code below and tell me where the error is? Or is there a better way of handling this? Thanks!

<cfif Len(trim(TotalFinishedSQFT)) GT 0>
<cfset priceperfoot = #Price# / #TotalFinishedSQFT#>
<cfelse></cfif>
0
Comment
Question by:Bang-O-Matic
  • 3
  • 2
6 Comments
 
LVL 14

Accepted Solution

by:
mds-cos earned 500 total points
ID: 39283242
Null is kind of funny, so I would recommend specifically checking for the null case as well as for 0:

<cfif TotalFinishedSQFT is NOT NULL>
  <cfif TotalFinishedSQFT GT 0>
...

Note that I did not use an AND to combine both comparisons.  I have run into problems when trying to do this with null values.
0
 

Author Closing Comment

by:Bang-O-Matic
ID: 39283291
<cfif TotalFinishedSQFT NEQ ''>
  <cfif TotalFinishedSQFT GT 0>

That fixed it! thanks for the help!
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39283300
@mds-cos - Yeah, gotta love those null values.  BTW, CF doesn't really have the concept of NULL's.  When the query comes back, they're handled as an empty string.  

@Bang-O-Matic

Two other options:

1) Use COALESCE in your db query to eliminate the nulls altogether:

         SELECT    COALESCE(TotalFinishedSQFT, 0) AS TotalFinishedSQFT
         FROM .....
2)  Use the val() function to convert it to 0. Then you don't have to do the extra check for an empty string.

<cfif val(TotalFinishedSQFT) GT 0>
       <cfset priceperfoot = #Price# / #TotalFinishedSQFT#>
</cfif>

The end result is the same as what mds-cos suggested, just throwing it out there as an FYI :)
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

Author Comment

by:Bang-O-Matic
ID: 39283321
Hi _agx_,
Thanks for your input. Is there a performance difference between any of these 3 solutions?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39283483
I ran a quick and dirty test comparing the double cfif vs the val() method, and the double cfif was a *little* slower. But it wasn't really noticeable until ~10k+ iterations - and even then it wasn't much. I wouldn't worry about it unless you're performing tens of thousands of comparisons.
0
 

Author Comment

by:Bang-O-Matic
ID: 39284337
Thanks _agx_
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question