8 Replies Latest reply on Apr 7, 2015 11:25 AM by gkaiseril

    division javascript

    klothos Level 1

      hello is this corect?

      // Get field values as numbers

       

       

          var v1 = +getField("B19").value;

       

       

          var v2 = +getField("Textfield37").value;

       

       

       

          // calculate this field value

       

       

          event.value = v1/v2;

        • 2. Re: division javascript
          try67 MVP & Adobe Community Professional

          No, actually. You have to first make sure that the divisor is not 0 or you'll get error messages because division by zero is not permitted.

          So the last line should be changed to:

           

          if (v2==0) event.value = "";

          else event.value = v1/v2;

          • 3. Re: division javascript
            klothos Level 1

            thanks but i would like the result to be only two decimal .

            but is like this 53.32564879999 .I want 53.32

            • 4. Re: division javascript
              try67 MVP & Adobe Community Professional

              Just set the field's Format setting to Number with 2 decimals, then.

              • 5. Re: division javascript
                hasand59089870 Level 2

                if the formatting of the fields is not possible, than try this:

                 

                var v1 = +getField("B19").value;

                var v2 = +getField("Textfield37").value;

                var c = v1/v2

                event.value =  (v2 != 0) ? (v1/v2).toFixed(2) : ""

                • 6. Re: division javascript
                  try67 MVP & Adobe Community Professional

                  toFixed will work too, but what's the point of the c variable in the code above?

                  • 7. Re: division javascript
                    hasand59089870 Level 2

                    Forget c. i have needed for my testing

                     

                    var v1 = +getField("B19").value;

                    var v2 = +getField("Textfield37").value;

                    event.value =  (v2 != 0) ? (v1/v2).toFixed(2) : ""

                    • 8. Re: division javascript
                      gkaiseril MVP & Adobe Community Professional

                      So you do not want rounding of the result.

                       

                      For the result without rounding I would use the Math.floor method for truncating the value as needed.

                       

                      function Truncate(nValue, nDed) {
                      // truncate a value to number of decimal places;
                      var nDecAdjust = Math.pow(10, nDec); // decimal adjustment value;
                      var nResult = nValue * nDecAdjust; // shift decimal point for truncation;
                      nResult = Math.floor(nResult); // truncate to intiger;
                      nResult = nResult /  nDecAdjust; // sift decimal point back;
                      return nResult; // return truncated value;
                      }

                      var v1 = +getField("B19").value;
                      var v2 = +getField("Textfield37").value;
                      var nDec = 2;
                      event.value =  v2 != 0 ?  Truncate(v1/v2, nDec) : "";

                      >> thanks but i would like the result to be only two decimal .

                      but is like this 53.32564879999 .I want 53.32

                       

                      This would truncate the value at 2 decimal places and not round to 2 decimal places.

                       

                      Using "toFixed(2)" provides the rounding and results in a value of 53.33 since the next digit in the answer is 5 and one rounds up at 5 or greater. To obtain the answer of 53.32 one would need to truncate the value at 2 decimal places.

                      There is no "truncation" function as such in JavaScript but one can write one. The Truncation function provided could be shortened but I have expanded it to show more clearly show the details of the calculation logic.

                       

                      This method sets the actual value of the calculation to 2 decimal places.

                       

                      Using the format tab option only sets the display value of the field rounded to the specified number of decimal palaces and not the actual value of the field.