Copy link to clipboard
Copied
I have an issue calculating a value correctly. But this involves a few fields to get to the answer.
- I provide four fields for the user to input numbers (A_TOTAL, B_TOTAL, C_TOTAL and D_TOTAL). I then take the sum of all these four numbers and it is provided in field G_TOTAL. All values are in this format ($x,xxx.xx)
- I first want the user to provide 3 dates (I have 3 date fields); StartDate, EndDate, and DateWithdrawal
- Based on the 3 dates provided in step 1, created 3 additional fields where I determine a value by using a calculation script.
For example:
- CompletedDays: I take the StartDate and DateWithdrawal to determine the difference in number of days
// Custom calculate script
(function () {
var sStart = getField("StartDate").valueAsString;
var sEnd = getField("DateWithdrawal").valueAsString;
var dStart, dEnd, diff;
if(sStart && sEnd) {
dStart = util.scand("m/d/yyyy", sStart);
dEnd = util.scand("m/d/yyyy", sEnd);
diff = dEnd.getTime() - dStart.getTime();
//Add one day to the difference
event.value = Math.floor(diff / 864e5)+1;
} else {
event.value = "";
}
})();
- TotalDays: I calculate the total number of days by taking the difference between the StartDate and EndDate fields
// Custom calculate script
(function () {
var sStart = getField("StartDate").valueAsString;
var sEnd = getField("EndDate").valueAsString;
var dStart, dEnd, diff;
if(sStart && sEnd) {
dStart = util.scand("m/d/yyyy", sStart);
dEnd = util.scand("m/d/yyyy", sEnd);
diff = dEnd.getTime() - dStart.getTime();
//Add one day to the difference
event.value = Math.floor(diff / 864e5);
} else {
event.value = "";
}
})();
- PercentEnrollCompleted: I take the CompletedDays field and divide that by the TotalDays (CompletedDays/TotalDays). The value reports a percent (example: 19.6%)
// Calculation script
(function () {
// Get field values as numbers
var numerator = +getField("CompletedDays").value;
var denominator = +getField("TotalDays").value;
// Perform calculation
if (denominator !== 0){
event.value = (numerator / denominator);
} else {
event.value = "";
}
})();
3. I have another calculated field called H_VALUE that evaluates the PercentEnrollCompleted field. If the percent is > 0.60, I assign a 100% , otherwise I report the percent if it's less than or equal to 0.60 (or 60%)
// Calculation script
(function () {
// Get field values as numbers
var v4 = +getField("PercentEnrollCompleted").value;
// Perform calculation
if (v4 > 0.60) {event.value = 1;
} else {
event.value = v4;
}
})();
So far it is working fine until I get to step 5 (next step):
5. This is where I'm having an issue, and it may be because of the various fields I'm using to determine this. In this step, I determine field I_TOTAL. Basically, I take the percent determined in Step 4 (H_VALUE) and multiply that by the number in Step 1 (G_TOTAL), but the number is not calculating correctly. As I update the date in step 3, the value in I_TOTAL is recalculated but again, it's not correct.
For example, when it multiplies these two fields, H_VALUE is 19.8% and G_TOTAL is 9,783.00, it calculates I_TOTAL as 2,030.43, but it should be 1,937.03. I can't seem to figure out where I am going wrong. Here is the calculated script I am using:
var f = this.getField("H_VALUE").value; // variable for the estimated field object
var g = this.getField("G_TOTAL").value; // variable for the actual field object
event.value = f * g; // multiply both values
Copy link to clipboard
Copied
I solved the issue, under the "Validate" tab, I selected "Run Custom Validation Script" and provided the following script:
if (event.valueAsString != "") event.value = util.printf("%.3f", +event.value);
That seemed to truncate the decimal value. Thanks Nesa, you gave me a pointer.
Copy link to clipboard
Copied
It's hard to tell whitout seeing actual file but you can try check if field calculation is correct ('Prepare form' -> 'More' -> 'set field calculation order') you can also try use 'Value is the' and select product(x) to check what result you will get.
Copy link to clipboard
Copied
So putting it in order helped a bit, it seems the value now being calculated in step 5 (I_TOTAL) is getting closer to the answer, however it is still off. In my existing example, I have 19.8% and multiplying that by $9,783.00. When using a calculator to do this, I come up with 1,937.03, however the calculated script in adobe is calculating 1,938.14. Of course, the calculation must be exact.
Here is the calculated script I have for field I_TOTAL:
var f = this.getField("H_VALUE").value; // variable for the estimated field object
var g = this.getField("G_TOTAL").value; // variable for the actual field object
event.value = f * g; // multiply both values
I'm attaching the form if it helps. It's on page 2, step 3.
Copy link to clipboard
Copied
Value in percentage field is not 0.198 it's 0.1981132 and when you multiply that you get 1,938.14.
Copy link to clipboard
Copied
oh, how do I truncate to 3 decimal places so that it is 0.198 instead of 0.1981132?
In the percent field (or H_VALUE) I have the format set to "Percentage" with Decimal Places set to "1", so that it shows up as 19.8. But it sounds that behind the scenes it is 0.1981132.
Copy link to clipboard
Copied
You can use toFixed() to round 0.1981132 to 3 decimals but that will give you 20% then.
How do you know 19.8% is correct result?
Copy link to clipboard
Copied
I solved the issue, under the "Validate" tab, I selected "Run Custom Validation Script" and provided the following script:
if (event.valueAsString != "") event.value = util.printf("%.3f", +event.value);
That seemed to truncate the decimal value. Thanks Nesa, you gave me a pointer.

