8 Replies Latest reply: Apr 6, 2012 3:03 AM by Quelluomo RSS

    Changing number of decimals depending on field value

    Quelluomo Community Member

      Hi everybody,

      I'm trying to make a form using Adobe Acrobat.

      I want a field that usually has only 2 decimal positions, to have 4 decimal positions ONLY when needed.

      Example: if field value is 3,53 it's fine, but if value is 3,5346 I want it to show it all.

      I've played around field properties and I've realized that I need to use a custom format script. I don't know where to start, though. Can you point me in the right direction?

      Thank you in advance.

        • 1. Re: Changing number of decimals depending on field value
          George_Johnson ACP/MVPs

          Exactly how is the field value being set? What if the value is 3.5300?

          • 2. Re: Changing number of decimals depending on field value
            Quelluomo Community Member

            if the value is 3.5300 it's the same as 3.53. I'd just want to avoid showing those extra zeroes (which keep showing if I set the decimals to 4)

            • 3. Re: Changing number of decimals depending on field value
              try67 ACP/MVPs

              Is this a calculated field? If so, then simply don't use any formatting

              settings at all, and it will automatically adjust the number of decimals

              shown to the most significant number.

              • 4. Re: Changing number of decimals depending on field value
                Quelluomo Community Member

                It's a currency value, and I need the decimals to be 2 at least. If the value is 3 it must show 3.00, not just 3.

                In some cases it's necessary to show more decimals because it actually matters with very big quantities.

                • 5. Re: Changing number of decimals depending on field value
                  George_Johnson ACP/MVPs

                  I can suggest a script, but it would help if you described exactly how you set up the current format. For example, what do you want to use for the currency character (if any), the thousands separator, and the decimal separator.

                  • 6. Re: Changing number of decimals depending on field value
                    Quelluomo Community Member

                    Currency is Euro ( € ). Thousands separator is the point ( . ) and decimal separator is the comma ( , )

                    Example: € 1.234,56

                    Even a simple direction will do. I'm not new to JS but I never used it into PDFs, as I mainly use it in webpages to manipulate the DOM and such.
                    I'm sure the solution is simpler than I think, but it's just confusing me at the moment

                    • 7. Re: Changing number of decimals depending on field value
                      George_Johnson ACP/MVPs

                      I just tossed the following together quickly and didn't do much testing, to take is as general guidance. It is intended to be placed in a document-level JavaScript and called from the field's custom Format script. You can't simply place it all as the Format script due to the way Acrobat behaves when you do.

                       

                      The point of the script is to calculate the number to use as the first parameter to the AFNumberFormant function, which is what Acrobat uses when you set up a numeric format. I couldn't think of a clever name for the function, so I'll leave that to you:

                       

                      function fmt1() {
                      
                          // Set the minimum number of digits to the right of the decimal
                          var min = 2;
                      
                          // Get the field value, as a string
                          var val = event.value;
                      
                          // Round to the nearest 10-thousand of a cent
                          var rn = util.printf("%.4f", val);
                      
                          // Replace any trailing zeroes with nothing
                          rn = rn.replace(/[0]+$/, "");
                      
                          // Get the number of characters after the decimal
                          var num = rn.split(".")[1].length;
                      
                          // Set to the minimum if appropriate
                          if (num < 2) num = 2;
                      
                          // Use built-in formatting function
                          AFNumber_Format(num, 2, 0, 0, "\u20ac", true);
                      
                      }
                      

                       

                      Call it like this in the field's Format event:

                       

                      fmt1();
                      

                       

                       

                      If this is not a calculated field, but one that a user will interact with, you'll want to add a corresponding Keystroke function, something like:

                       

                      function keystroke1() {
                      
                          AFNumber_Keystroke(2, 2, 0, 0, "\u20ac", true);
                      
                      }
                      
                      • 8. Re: Changing number of decimals depending on field value
                        Quelluomo Community Member

                        Works like a charm!

                         

                        I just replaced this:

                        // Set to the minimum if appropriate
                            if (num < 2) num = 2;
                        

                         

                        with this:

                        // Set to the minimum if appropriate
                            if (num < min) num = min;
                        

                         

                        because I thought it's what you meant.

                        Thank you so much, I can't tell you how grateful I am.

                        Should you ever come to Italy, I owe you a beer