6 Replies Latest reply: May 29, 2012 2:17 PM by GKaiseril RSS

    Calculations with Rounded Numbers

    MJBateman1

      I've created a form in Adobe X that has a few calculated fields.  For ease of conversation, we'll call them Field A, Field B and Field C (see below).  I need Fields A and B to be rounded to the nearest whole number so that Field C is also a whole number.  I've tried formatting Fields A and B to show no decimals but the calculaton is still being performed on the non-rounded numbers.  Can anyone help with this?  Thanks.

       

      Field A - Field B = Field C

        • 1. Re: Calculations with Rounded Numbers
          George_Johnson CommunityMVP

          You'll have to use JavaScript. The code for the custom calculation script for your example would be:

           

          // Cust calculation script

          (function () {

           

              // Get the field values, rounded to nearest integer

              var v1 = Math.round(+getField("fieldA").value);

              var v2 = Math.round(+getField("fieldB").value);

           

              // Set this field value to the difference

              event.value = v1 - v2;

           

          })();

           

           

          Replace "fieldA" and "fieldB" in the code above with the actual field names.

          • 2. Re: Calculations with Rounded Numbers
            GKaiseril CommunityMVP

            Well this is the way JavaScirpt works and for many calculations the unrounded nubmers are needed for the calculaitons.

             

            You need to set the value of the calculated field to the rounded value.

             

            I would use a custom calculation script and you can use JavaScript's "Math.round" method or Acrobat's "util.printf" method.

             

            event.value = Math.round(event.value);

             

            event.value - util.printf("%,1 .0f", event.value);

            • 3. Re: Calculations with Rounded Numbers
              MJBateman1 Community Member

              George I must be missing something.  I added the math.round functions as shown in your example but now it doesn't calculate at all.  Can you look at the coding shown below and let me know where I went wrong?  Thanks.

               

              Updated Coding:

              (function(){

              var totalMiles = math.round(+this.getField("Total Miles DrivenRow1").value);

              var homeMiles = math.round(+this.getField("Home Base MilesRow1").value);

              var diff = totalMiles - homeMiles;

              if (diff<0) diff = 0;

              event.value = diff;})();

               

              Original Coding:

              var totalMiles = +this.getField("Total Miles DrivenRow1").value;

              var homeMiles = +this.getField("Home Base MilesRow1").value;

              var diff = totalMiles - homeMiles;

              if (diff<0) diff = 0;

              event.value = diff;

              • 4. Re: Calculations with Rounded Numbers
                MJBateman1 Community Member

                GKaiseril are you saying you think I can just add the math.round function to the "event value" at the end of the coding?

                • 5. Re: Calculations with Rounded Numbers
                  George_Johnson CommunityMVP

                  It's not math.round, it's Math.round.

                  • 6. Re: Calculations with Rounded Numbers
                    GKaiseril CommunityMVP

                    Yes you can, and I would do it for each calculated field where you want the rounded value of the calculation used.

                     

                    If you use the Math object's round method you use:

                     

                    Math.round(FloatingPointValue)

                     

                    "Math" has to have the first letter capitalized.