Solved

Coldfusion/MySql error - Division by zero is not possible

Posted on 2013-06-27
6
508 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now