Copy link to clipboard
Copied
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?
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);
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
@+
Copy link to clipboard
Copied
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 = "";
else //Weight/(Height/100*Height/100)
I have tried both of the above scripts.
Copy link to clipboard
Copied
You can't use the scripts in the simplified field notion.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
Hi Nesa,
This works perfectly and Ive been able to adjust it for my other calulations.
Thank you very much!!