Copy link to clipboard
Copied
I am trying to add two formulas to my pdf form:
The first formula is for BMI which is 703*weight in pounds/height inches squared. I made the BMI box a number initially but an error came up that said fields don't match. I Imagine it is either because it is a long decimal or because there is a zero in the denominator until someone types a value in there. I changed the field to reflect "none" in formatting and now it is showing "NaN" until something is typed. I would like for "NaN" not to show.
I have ZERO knowledge of javascript, and just need to know what to type in and where. I figure the solution to the first formula will help me solve the second formula issue.
Copy link to clipboard
Copied
See if this works for you:
https://drive.google.com/uc?export=download&id=16Rm7Vy4-ua4a6e7RTgRk_lsbsCMGvRHH
Copy link to clipboard
Copied
Format field as number with 2 decimals.
Copy link to clipboard
Copied
In your file you are missing parentheses at the end.
Copy link to clipboard
Copied
Since those fields have calculations they won't be blank so there is no point to check if field is "" and since you can't divide with 0 you need to check that divisor in this case field "Text14" is not 0 for calculation to trigger, like this:
if(this.getField("Text14").value == 0)
event.value = "";
else
event.value = Number(this.getField("Text13").value)/Number(this.getField("Text14").value);
Copy link to clipboard
Copied
Instead of Simplified field notation use this as custom calculation script:
var c1 = Number(this.getField("Calc1").value);
var c2 = Number(this.getField("Calc2").value);
var c3 = Number(this.getField("Calc3").value);
var x = c1+c2+c3;
if(x == 0)event.value = "";
else
event.value = 18000/(2*x);
Copy link to clipboard
Copied
Post script you are using.
Copy link to clipboard
Copied
This is the script: 703*pg8_2/(pg8_1*pg8_1). I've input this is the simplified field notation
Copy link to clipboard
Copied
See if this works for you:
https://drive.google.com/uc?export=download&id=16Rm7Vy4-ua4a6e7RTgRk_lsbsCMGvRHH
Copy link to clipboard
Copied
Yes! this works. Is there a way to limit the answer to two decimal points?
Copy link to clipboard
Copied
Format field as number with 2 decimals.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
In your file you are missing parentheses at the end.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Checking if the fields are empty is not enough. You also need to make sure the second field's value is not zero. See the code I posted below.
Copy link to clipboard
Copied
Since those fields have calculations they won't be blank so there is no point to check if field is "" and since you can't divide with 0 you need to check that divisor in this case field "Text14" is not 0 for calculation to trigger, like this:
if(this.getField("Text14").value == 0)
event.value = "";
else
event.value = Number(this.getField("Text13").value)/Number(this.getField("Text14").value);
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Instead of Simplified field notation use this as custom calculation script:
var c1 = Number(this.getField("Calc1").value);
var c2 = Number(this.getField("Calc2").value);
var c3 = Number(this.getField("Calc3").value);
var x = c1+c2+c3;
if(x == 0)event.value = "";
else
event.value = 18000/(2*x);
Copy link to clipboard
Copied
NaN = Not a Number
This is a Format issue, can you share (part of) your document?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Use this code as the field's custom calculation script:
var v1 = Number(this.getField("pg8_1").valueAsString);
var v2 = Number(this.getField("pg8_2").valueAsString);
if (v1==0) event.value = "";
else event.value = (703*v2/(v1*v1));
Copy link to clipboard
Copied
This typically happens when you divide a number by zero and try to apply the result to a Number field.
To avoid it you must use a script. If you post the exact formula you're using for that field's calculation we can help you convert it into such a script.
Copy link to clipboard
Copied
This is the script: 703*pg8_2/(pg8_1*pg8_1)
I've input this is the simplified field notation
Copy link to clipboard
Copied
As we assumed:
- input fields should have the "Number" format to avoid unwanted entries.
- the Simple Field Notation can't protect against division by zero or division by nothing, so you must use a JavaScript calculation.
Joker: calculated fields should be read only.
😉
Copy link to clipboard
Copied
great. How do I add a tofixed(2) vaue to this script:
if(this.getField("pg8_4").valueAsString == "" || this.getField("pg8_5").valueAsString == "")
event.value = "";
else
event.value = Number(this.getField("pg8_4").value)/Number(this.getField("pg8_5").value);
Find more inspiration, events, and resources on the new Adobe Community
Explore Now