12 Replies Latest reply on Oct 3, 2008 5:29 AM by (LauraLeslie)

    util.printf()

    Level 1
      Hi,

      I would like to format the number like:

      "123456.7890123456" -> "123,456.7890123456"

      I try printf("%,0f") but it displays "123,457."

      Would you guys teach me how to format the number please?

      Thanks!!!
        • 1. Re: util.printf()
          Bernd Alheit Adobe Community Professional & MVP
          Use a format string like this: "%,0.2f"
          • 2. Re: util.printf()
            Level 1
            Bernd Alheit wrote:
            > Use a format string like this: "%,0.2f"


            The customer wants to keep all the decimal... without any trailing zero

            if I have "1000", the guy wants to display "1,000"
            not "1,000.00"

            if I have "1000.1", the guy wants "1,000.1"
            not "1,000.10"

            any simple trick to do this?
            • 3. Re: util.printf()
              Bernd Alheit Adobe Community Professional & MVP
              When you want "1,000.1" then use "%,0.1f".
              • 4. Re: util.printf()
                I am attempting to force a value in a field to 15 characters, left zero padded when necessary. The following code works with an all number value, but if the value has letters, it does not. I'm not sure how the first parameter works in this function, nor do I know all the variations which can be used in the first parameter. I'm assuming the "d" in "%015d" stands for decimal?

                var AccNum = util.printf("%015d", (this.getField("AccountNumber").valueAsString));
                • 5. Re: util.printf()
                  gkaiseril MVP & Adobe Community Professional
                  > I'm assuming the "d" in "%015d" stands for decimal?

                  No, it stands for integer and it will be truncated as necessary.

                  The full syntax is:

                  util.printf("%[,nDecSep][cFlags][nWidth][.nPrecision]cConvChar", cString);

                  cDecSep, preceded by a comma character (,), is a digit from 0 to 3 which indicates the decimal/
                  separator.
                  2 - period separated, comma decimal point.

                  cFlags is only valid for numeric conversions and consists of a number of characters (in any
                  order), which will modify the specification:
                  0 - specifies padding to the field with leading zeros.

                  nWidth is a number specifying a minimum field width.

                  nPrecision, preceded by a period character (.), is a number which specifies the number of
                  digits after the decimal point for float conversions.

                  cConvChar is one of:
                  d - integer, interpret the argument as an integer (truncating if necessary).
                  f - float, interpret the argument as a number.

                  So you can use:

                  var AccNum = util.printf("%015.0f", this.getField("AccountNumber").value);

                  More information is included in the Acrobat JavaScirpt Reference, http://www.adobe.com/devnet/acrobat/javascript.html .
                  • 6. Re: util.printf()
                    Level 1
                    That's great. Thank you very much for your explanation.
                    • 7. Re: util.printf()
                      Level 1
                      Not sure if you are still around Geo Kaiser. The code you suggested ie.
                      var AccNum = util.printf("%015.0f", this.getField("AccountNumber").value);

                      does not work when there is a letter in the field value. As I suggested,

                      var AccNum = util.printf("%015d", (this.getField("AccountNumber").valueAsString));

                      works when the value in the field is numbers only. If there are letters, it does not work. Can the util.printf function be used with letters? Example: The field value is CS123456789 - I would like the result of this value to be 0000CS123456789 ie.zero padded.

                      Thanks.
                      • 8. Re: util.printf()
                        George_Johnson MVP & Adobe Community Professional
                        I'd suggest a different approach:


                        // Fifteen zeroes
                        var s1 = "000000000000000";

                        // Get field value
                        var s2 = getField("AccountNumber").valueAsString;

                        // Add needed number of leading zeroes
                        var AccNum = s1.substr(0, s1.length - s2.length) + s2;


                        George
                        • 9. Re: util.printf()
                          gkaiseril MVP & Adobe Community Professional
                          The code I provided will not work with any string that has a non-numeric or period in it, because the value is not a floating point number but is an Alphanumeric value and require the approach suggested by George Johnson.
                          • 10. Re: util.printf()
                            Level 1
                            Thanks very much George. This might just work. I'll let you know. :o)
                            • 11. Re: util.printf()
                              Level 1
                              Thanks again Geo. This is good to know. I'm trying Georges suggestion right now.
                              • 12. Re: util.printf()
                                Level 1
                                She works like a charm. Thanks again!!