16 Replies Latest reply on May 31, 2018 9:01 PM by George_Johnson



      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?

        • 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/
                  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.

                      • 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;

                        1 person found this helpful
                        • 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!!
                                • 13. Re: util.printf()

                                  Thanks, George!

                                  I did a lot of searching and found only a couple of potential solutions:

                                  INDEX = util.printf("%04d", INDEX);

                                  INDEX = "%04d".sprintf(INDEX);

                                  Neither of these worked, unfortunately. But, your solution is a great workaround!


                                  In my script, I have to first change the INDEX value to a number, increment it and then use your solution to add the padding:


                                  // Convert to number so we can increment it...

                                  INDEX = "0001"

                                  INDEX = Number(INDEX);


                                  // Increment...



                                  // Convert back to string, so we can add padding (since there doesn't seem to be a method that works)...

                                  INDEX = INDEX.toString();


                                  // Add needed number of leading zeroes...

                                  // Four zeroes padding...

                                  var ZEROS = "0000";

                                  INDEX = ZEROS.substr(0, ZEROS.length - INDEX.length) + INDEX;

                                  • 14. Re: util.printf()
                                    try67 MVP & Adobe Community Professional

                                    util.printf does work. This code returns "0001":

                                    util.printf("%04d", 1);

                                    • 15. Re: util.printf()
                                      rkbobo Level 1

                                      Hmm... When I run it in ExtendScript Toolkit, it just beeps at me.

                                      • 16. Re: util.printf()
                                        George_Johnson MVP & Adobe Community Professional

                                        The ExtendScript toolkit doesn't work with Acrobat, but Acrobat does have an interactive JavaScript console that allows you to enter and execute code.