2 Replies Latest reply on Oct 15, 2012 2:00 PM by gkaiseril

    Calculated Field with currency formatting and hiding zero values


      I am designing a PDF form for sales quotes and I have a field set to multiply the quantity of an item and the per unit price to give the extended price.  There are 5 lines on each quote and if the users are only quoting 1 or 2 items I want the zero values in the unused extended price fields to be hidden.


      I did that with the following script


      if (event.value == 0) event.value = '';


      But when I enter something in the quantity and price the extended price is displayed as a plain number (100) instead of currency ($100).


      I can format it to change to currency with


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


      I am sure there is a way to combine them to result in an if then situation, so if the field is 0, make null and if not then format as currency but I am not sure how to do it.


      Any help would be appreciated.



        • 1. Re: Calculated Field with currency formatting and hiding zero values
          George_Johnson MVP & Adobe Community Professional

          You didn't say where you're using either of those scripts, so it's hard to say what's wrong. But normally you'd set the formatting the way you want it (on the Format tab), and can then use the following as the calculated field's custom Validate script:


          // Custom Validate script for calculated field

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


          Note that if the result of the calculation could ever validly result in 0, then it's best to deal with this in a custom calculate script.


          If you are using a custom calculation script (as opposed to one of the built-in calculation methods), you can blank the field in the calculation script and not use such a Validate script:


          // Custom calculation script fragment

          event.value = "";

          • 2. Re: Calculated Field with currency formatting and hiding zero values
            gkaiseril MVP & Adobe Community Professional

            The newer versions of Acrobat hide the zero values for fields using the Number format and using a currency symbol.'


            But since you can not control the updating of user systems, you need to use a little documented function for formatting numeric fields on the fly.


            With the extended price fields set to a format of "None" you can add the following script to the "Custom Validation" script or add it to a Custom JavaScript calculation. The "Custom Validation will work with any calculation method. Note the form needs to be cleared or recalculated to see the result of validation script change.


            if(event.value == 0) {
            event.value = ""; // set field value to null for a zero value
            AFNumber_Format(2, 0, 0, 0, "", true); // format for number with no currency symbol
            } else {
            AFNumber_Format(2, 0, 0, 0, "$", true); // for non-zero values format for number with currency symbol


            The positional parameters are Number of Decimals, Separator Style, Negative Value Style, Currency Style (unused), string Currency Symbol, Pre-pend Currency Symbol.