Copy link to clipboard
Copied
I know there are a million questions about this. I swear I've read them all. They are all nuanced and for a completely illiterate JavaScript person, I'm dumfounded.
I have a form with several division calculations. The first is calculating correctly, when information is put into the fields, but before I get to the calculation in question, every other field I type into gives me the error.
I need to divide two numbers (both whole numbers) and the answer needs to be a percentage. So my first field "A" has no formatting, the second field "B" also has no formatting. The quotient field is formatted as a percentage. Here is my JavaScript formula:
event.value = (this.getField("TenantSqFt").value / this.getField("ProjectSqFt").value);
I read somewhere that calculations don't like formatting. That's great, but when I take away the percentage formatting for the quotient field it gives me a very lengthy decimal. Note that I do not get the error at all when I take the percentage formatting off the quotient field.
So either I'm doing something wrong, or I need to adjust the Java Script formula to adjust for this. I can change the formula for it to appear like a percent by multiplying by 100, but I still get the lengthy decimal:
event.value = ((this.getField("TenantSqFt").value / this.getField("ProjectSqFt").value) * 100);
Ideally my A and B fields would be able to be formatted as numbers (so they can have commas for the thousands place) but if that's a deal breaker I'll have to live with it.
So then the next question becomes, do I need to throw rounding into the mix? And how on earth do I do that??
Thank you fine folks!!
Copy link to clipboard
Copied
Use this:
var a = Number(this.getField("TenantSqFt").valueAsString);
var b = Number(this.getField("ProjectSqFt").valueAsString);
var x = (a/b)*100;
if(b == 0) event.value = "";
else
event.value = x.toFixed(2);
Copy link to clipboard
Copied
Thats because you try to division with zero, try like this:
if(Number(this.getField("ProjectSqFt").valueAsString) == 0)
event.value = "",
else
// your code goes here
You can use "toFixed()" to round to how many decimals you want.
Copy link to clipboard
Copied
Thank you for your reply. I'm a complete no-nothing about JavaScript, so I'm not sure exactly what to type. I tried this, but no result.
if(number(this.getField("TenantSqFt/ProjectSqFt").valueAsString) == 0) event.value = ToFixed (((this.getField("TenantSqFt").value / this.getField("ProjectSqFt").value) * 100),2);
Copy link to clipboard
Copied
Use this:
var a = Number(this.getField("TenantSqFt").valueAsString);
var b = Number(this.getField("ProjectSqFt").valueAsString);
var x = (a/b)*100;
if(b == 0) event.value = "";
else
event.value = x.toFixed(2);
Copy link to clipboard
Copied
I feel very dense, but I tried this and I'm getting a Syntax Error...
if(Number(this.getField("ProjectSqFt").valueAsString); == 0) event.value = ""; else event.value = (Number(this.getField("TenantSqFt").valueAsString); / Number(this.getField("ProjectSqFt").valueAsString);) * 100;.toFixed(2);
Copy link to clipboard
Copied
There are multiple errors in your script.
Is there reason you don't use script I gave you?
Copy link to clipboard
Copied
Semi-colons should only be added at the end of each line, not in the middle of it.
Copy link to clipboard
Copied
Thank you. I was making it harder than it needed to be. Your script exactly as you wrote it works perfectly.
Copy link to clipboard
Copied
Hey, I don't know if you are still doing this, but I am trying something similar and cannot get the code to work.
What I am doing is 2 variables divided by each other, then divided by 100. Here is what I have of the script to you gave:
var a = Number(this.getField("ACTROI1").valueAsString);
var b = Number(this.getField("AmountReq").valueAsString);
var x = (a/b)/100;
if(b == 0) event.value = "";
else event.value = x.toFixed(2);
Both variables will be 0 at the start of the form, I normally have the field of the calculation formatted to percentage, I have tried changing it so see if that would work, but no luck.
Can you please help?
Copy link to clipboard
Copied
Ok, I don't know how to math, so I need to Multiply by 100, still doesn't work when trying that.
Copy link to clipboard
Copied
Check the Javascript console (ctrl-j) for errors.
Copy link to clipboard
Copied
Finally figured out how to get the console up, it would not work on my other laptop. The errors are:
TypeError: getField(...) is null
2:AcroForm:Text1:Calculate
Copy link to clipboard
Copied
The error message means that one of the fields doesn't exist.
Copy link to clipboard
Copied
I have check the field name 3 times and it keeps doing that. But that's alright I managed to find some different code that works for my form. Thank you both for your help.
Copy link to clipboard
Copied
What exactly doesn't work, field doesn't show result or result is incorrect?
Check that field names are correct.
If you have field format as percentage, you don't need toFixed() just set in format to 2 decimals.
Copy link to clipboard
Copied
The field names are correct (I copy and pasted the names), and I have tried with and without the formatting. It doesn't seem to calculate anything when I have the fields filled in.
Copy link to clipboard
Copied
Can you share the file?
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more