JavaScript

Currently Being Moderated

Java Script Problem - percent and decimal points

Apr 12, 2012 8:34 PM

Adobe Acrobat Java Script Percent Equation Problem

1. event.value=(this.getField("AmountActualstoDateRestaurant2").value-thi s.getField("AmountPriorYearToDateVarRestaurant3").value)/this.getField ("AmountPriorYearToDateVarRestaurant3").value;

Replies
• 11,659 posts
Aug 11, 2002
Currently Being Moderated
Apr 13, 2012 10:22 AM   in reply to bsommerhardees

Here's one way:

``````(function () {

// Get the field values, as numbers
var v1 = +getField("AmountActualstoDateRestaurant2").value;
var v2 = +getField("AmountPriorYearToDateVarRestaurant3").value;
var v3 = +getField("AmountPriorYearToDateVarRestaurant3").value;

// Calculate the value
if (v3 !== 0) {
event.value = util.printf("%.2f", 100 * (v1 - v2) / v3) + "%";
} else {
// Display nothing when denominator is zero
event.value = "";
}

})();
``````

If you set up the field with a percent format, just leave out the multiplication by 100 and the addition of the percent sign:

``````        event.value = util.printf("%.2f", (v1 - v2) / v3);
``````

|
Mark as:
Actions

More Like This

• Retrieving data ...