4 Replies Latest reply on Jan 22, 2009 2:49 PM by gkaiseril

    How Can A Numeric Number Be Stripped To Two Places After A Decimal?

    Jim MacD
      Hi,

      Im looking for code to strip a Numeric String to two places after the decimal.

      Thanks for any help.
        • 2. Re: How Can A Numeric Number Be Stripped To Two Places After A Decimal?
          gkaiseril MVP & Adobe Community Professional
          Do you want to control the display or the actual value.

          For the display, just set the number of decimals to display.

          For modifying the result of a calculation or entry, you can use the JavaScript "Math.round()" method or the "util.printf()" method for rounding and the "Math.floor()" method for truncation.

          Rounding to 2 decimal places:

          console.show();
          console.clear();
          var fInput = this.getField("input").value; // get field value
          console.println("fInput: " + fInput);
          var fNumber100 = fInput * 100; // shift decimal for conversion to intiger
          console.println("fInput * 100: " + fNumber100)
          var iNumber = Math.round(fNumber100); // round integer
          console.println("iNumber: " + iNumber)
          var fNumber = iNumber / 100; // restore decimal point
          console.println("Rounded number: " + fNumber);

          // or

          console.show();
          console.clear();
          var fInput = this.getField("input").value; // get field value
          console.println("fInput: " + fInput);
          var fNumber = util.printf("%,0 .2f", fInput); // round to 2 decimal
          console.println("Rounded number: " + fNumber);

          Truncation to 2 decimal places"

          console.show();
          console.clear();
          var fInput = this.getField("input").value; // get field value
          console.println("fInput: " + fInput);
          var fNumber100= fInput * 100; // move decimal point
          console.println("fInput * 100: " + fNumber100)
          var iNumber = Math.floor(fNumber100); // truncate to intiger
          console.println("iNumber: " + iNumber)
          var fNumber = iNumber / 100; // restore decimal point
          console.println("Truncated number: " + fNumber);
          • 3. Re: How Can A Numeric Number Be Stripped To Two Places After A Decimal?
            Jim MacD Level 1
            I'm too new. Can anyone be more specific and give me an example where 'Text1" is the name of my form field?
            • 4. Re: How Can A Numeric Number Be Stripped To Two Places After A Decimal?
              gkaiseril MVP & Adobe Community Professional
              You have a text field with a name of "Text1".

              You now have many choices. But let's keep this to the "Format" tab set to "Number" and the "Decimal places" to 2. But the data entered or computed for that field can have more data than the 2 decimal places. You can either use the "Validate" or "Calculation" tabs to add a script to perform the conversion, but do not use both. You can use the following script for either the "Run custom validation script' or the "Custom calculation script":

              event.value = 1 * util.printf("%,1 .2f", event.value);