Hi I have had a read few of the forums around this topic but havent found what I am looking for. I am very new to this and am looking for a simple solution.
I have two calculations that I need to do:
1. Weight / height^2 (which I have written as (Weight/(Height/100*Height/100)) as we write our height in cm not m
2. Waist / Hip
I am able to get the calclautions working however I need them to be to 2 decimal places which can be changed under the format tab however the above error message keeps coming up - I know this comes up because you cant divide something by 0. What script can I insert into this to make sure this message doesnt keep coming up?
Check for 0 in your calculation and set field empty or 0 if it's true, something like this:
if(this.getField("field").value == 0) event.value = "";
else //calculation goes here
of course this is just basic way to do it, it would be helpful if you posted your actual script.
The result can be 0, but 0/0=NaN and 1/0=Infinity, so write instead:
if(!isFinite(this.getField("field").value)) event.value = ""; else //calculation goes here
This is what I have inserted into both the simplified field notion and custom calculation script but have not had any luck with it calcuating.
if(!isFinite(this.getField("field").value)) event.value = "";
I have tried both of the above scripts.
You can't use the scripts in the simplified field notion.
Field name "field" is just example you need to input your field name,also whats are Weight and Height,field names or variables? What are names of two fields you try to calculate and do you use any formats in them?
Hi Nesa the field names are 'Height' and 'Weight'. The field name I want the calculation in is 'BMI'.
No formats in height and weight however I would like to format the BMI to 2 decimal places.
You can format "BMI" as number with 2 decimal under format tab, and try this code,put it into 'Custom calculation':
var w = this.getField("Weight");
var h = this.getField("Height");
if(isNaN(w.value) || isNaN(h.value) || w.value == 0 || h.value == 0) event.value = "";
else event.value = w.value/(h.value/100*h.value/100);
This works perfectly and Ive been able to adjust it for my other calulations.
Thank you very much!!