Copy link to clipboard
Copied
I have 2 fields for the result of a division: the whole number and the decimal (1 decimal). I Math.floor the result to get the whole number and all goes well until I hit a case where the result is 92.97.
This should give "93" in the whole field and "0" in the decimal field, instead it gives me "92" and "10" (even though I only allow 1 character in the field).
How do I get the "93" and "0" in the following code?
fieldLVEDV = this.getField("LVEDV").value;
fieldBSA = this.getField("BSA").value;
if (fieldLVEDV != "" && fieldBSA != ""){
totalLVEDV = this.getField("LVEDV").value + ((this.getField("LVEDVDec").value)/10);
calculation = (totalLVEDV / fieldBSA);
calculationDecimal = calculation;
partWhole = Math.floor(calculation);
if(partWhole <0) {partWhole ="";}
this.getField("Index").value = partWhole ;
this.getField("IndexDecimal").value = Math.round(10*(calculationDecimal-partWhole));
{if (this.getField("IndexDecimal").value = "10")
{
this.getField("Index").value = partWhole+1;
this.getField("IndexDecimal").value = "0"
}}
} else {
this.getField("Index").value = "";
this.getField("IndexDecimal").value = "";
}
I thought adding the blue part would do the job but obviously it didn't work.
Ah, your problem is that you are not using the correct comparison operator. To check if two values are equal, the operator is "==" an not not "=". By using "=", you are assigning the value 10 to the field, which you are then changing to 0 a couple of lines down. Use "==" and things should work correctly.
Copy link to clipboard
Copied
What data do you have in your input fields (LVEDV, LVEDVDec and BSA) for the problem to occur?
Copy link to clipboard
Copied
LVEDV: 178
LVEDVDec: 5
BSA: 1.92
Copy link to clipboard
Copied
It looks like you are doing the right thing with adjusting the partWhole variable by one if your number of decimals is 10. It also gives me the correct output when I plug that into a simple form.
What application are you using when the result is incorrect?
Copy link to clipboard
Copied
I get a result of "92" and "1"
I use Adobe Acrobat Pro 10. I put the code in the "On Blur" action of the LVEDV and LVEDVDec fields.
Copy link to clipboard
Copied
It also works for me in Acrobat X. Are you sure that both scripts are the same? I would create a document level script and call that from the individual on-blur actions. Alternatively, you can also create a new hidden and read-only field, and use your script as the custom calculation script.
Copy link to clipboard
Copied
Do you get 92.7 (178/1.92) before entering the 5 in the decimal field to make it 178.5?
If I remove the blue script, I would get 92.7 for 178 (correct answer), and 92.1 for 178.5
With the blue script, I get 93.0 for both 178 and 178.5.
Copy link to clipboard
Copied
Ah, your problem is that you are not using the correct comparison operator. To check if two values are equal, the operator is "==" an not not "=". By using "=", you are assigning the value 10 to the field, which you are then changing to 0 a couple of lines down. Use "==" and things should work correctly.
Copy link to clipboard
Copied
That worked well. Thanks
Copy link to clipboard
Copied
I would use the util.printf() method to round the result of the division and then the modulo operator to get the decimal fraction.
fieldLVEDV = this.getField("LVEDV").value;
fieldBSA = this.getField("BSA").value;
if (fieldLVEDV != "" && fieldBSA != ""){
// perform the divison and roud the result;
// calculation = (totalLVEDV / fieldBSA) rounded to 1 decimal place;
totalLVEDV = util.printf("%,1 1.1f", fieldLVEDV / fieldBSA);
console.println(totalLVEDV);
// get the whole number protion of the result;
var partWhole = Math.floor(totalLVEDV);
var partDecimal = Math.floor((totalLVEDV % 1) * 10); // remainder when divided by 1;
console.println( partWhole + " " + partDecimal);
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now