Skip navigation
bsommerhardees
Currently Being Moderated

Java Script Problem - percent and decimal points

Apr 12, 2012 8:34 PM

Adobe Acrobat Java Script Percent Equation Problem

 

Please help me finish this script to result in an answer that is a percent with 2 decimal points. 

 

  1. event.value=(this.getField("AmountActualstoDateRestaurant2").value-thi s.getField("AmountPriorYearToDateVarRestaurant3").value)/this.getField ("AmountPriorYearToDateVarRestaurant3").value;
 
Replies
  • George Johnson
    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:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points