Expand my Community achievements bar.

Formcalc Calculations - Division by ZERO

Avatar

Level 10
I am having difficulty with an if statement to prevent division by zero before the user puts in a value for the "Hours_Direct5" field



if ( Hours_Direct5=0 ) then Fatal_AFR_Direct5=0 else Fatal_AFR_Direct5 = Number_Fatalities_Direct5 * 100000 / Hours_Direct5 endif



For starters it did not like the statement "if ( Hours_Direct5=0 )" so I changed it to "if ( Hours_Direct5<1 )" and set a Default value for Hour_Direct5 as 0. While this works it is not particurarly good, as having a ZERO value for the Hours_Direct5 discourages a user putting in a real value.



Ideally I want the IF statement to return a blank statement " " if the Hours_Direct5 field is either null or zero (i.e. if the user has not put in a value for the hours)



Any help gratefully received, thanks, Niall (a script novice)
4 Replies

Avatar

Former Community Member
You're very close but there are a couple of little details you're missing in your script. Here's what I think it should be (FormCalc script on Fatal_AFR_Direct5 object's Calculate event):



if (Hours_Direct5 == null OR Hours_Direct5 == 0) then

Fatal_AFR_Direct5 = ""

else

Fatal_AFR_Direct5 = Number_Fatalities_Direct5 * 100000 / Hours_Direct5

endif


This will set Fatal_AFR_Direct5's value to blank unless both the Hours_Direct5 and Number_Fatalities_Direct5 objects are filled-in with numbers.



Stefan

Adobe Systems

Avatar

Level 10
Works perfectly. Thank you very much Stefan.

Avatar

Former Community Member
Niall_O_Donovan@adobeforums.com wrote:

> I am having difficulty with an if statement to prevent division by zero before the user puts in a value for the "Hours_Direct5" field

>

> if ( Hours_Direct5=0 ) then Fatal_AFR_Direct5=0 else Fatal_AFR_Direct5 = Number_Fatalities_Direct5 * 100000 / Hours_Direct5 endif

>

> For starters it did not like the statement "if ( Hours_Direct5=0 )" so I changed it to "if ( Hours_Direct5<1 )" and set a Default value for Hour_Direct5 as 0. While this works it is not particurarly good, as having a ZERO value for the Hours_Direct5 discourages a user putting in a real value.

>

> Ideally I want the IF statement to return a blank statement " " if the Hours_Direct5 field is either null or zero (i.e. if the user has not put in a value for the hours)

>

> Any help gratefully received, thanks, Niall (a script novice)



Niall,

Try using the following:

if ( Hours_Direct5 == 0)



When you use a single equal sign, you are assigning Hours_Direct5 equal

to zero. When you use a double equal sign, you are testing the two

values to see if they are equal.



Justin Klei

Cardinal Solutions Group

www.cardinalsolutions.com

Avatar

Level 10
Thanks, now I know "how, but also "why" ;-)