Announcement

Collapse
No announcement yet.

Calculating percentage

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculating percentage

    Ho do I create a box that when I add a number, it subtracts that percentage from the cell above it? I want to subtract 15% from the cell amount of currency above it.
    i.e. 15% of $258.00

  • #2
    Place this as a calulation script in the target field, you must adapt the above field's name:

    var nVal = Number(this.getField("cell_above").value) * 0.15;
    event.target.value = Number(this.getField("cell_above").value) - nVal;

    Comment


    • #3
      Thank you JRB. The script works but as a newbie I would like to work it this way. I have 3 boxes stacked. The top one is 'subtotal', the next one down I want to type in the percentage of discount which for now is 15%, and the bottom box is the 'Total,', which then shows the total with the 15 % discount. The script you gave me works fine but it doesn't connect the the middle discount box where I would want to type in the amount of discount, which for now is 15%. For now, the 'Discount box ' is dead so I just put any number in there which is fine because your script works for the total box but it be great if I can type in a number the discount box and it automatically calculates it for the total box. Hope this makes sense. Thanks

      Comment


      • #4
        JACKIEJAY
        I will submit your request to our engineer team and create a new JS for you, will back to you soon!

        Comment


        • #5
          Here is it:

          var nDiscount = this.getField("discount").value;
          var nPrice = this.getField("cell_above").value;

          var nVal = Number(nPrice) * Number(nDiscount);
          event.target.value = Number(nPrice) - nVal;

          Comment


          • #6
            Thanks so much for your effort. Not quite sure what part of scripts go into which boxes calculations fields. The Subtotal Box, Discount amount box, and the Total amount box. Plus the appropriate edits of your script to make it work.

            Click image for larger version

Name:	Screenshot 2021-02-23 113911.jpg
Views:	100
Size:	19.3 KB
ID:	181711

            Comment


            • #7
              The script above cannot work, I made a mistake.
              Use this one as a Calculation script in the "TOTAL" field. Since I don't know what is the Format used by this field, I assume that the value returned by the DISCOUNT field is 15, or 15%


              var nDiscount = this.getField("DISCOUNT").value;
              var nPrice = this.getField("SUBTOTAL").valueAsString.replace(/\%/gim,"");
              // remove the % car. if exist

              if (nPrice.length > 1) {var nZero = "";}
              else {var nZero = "0";}
              var nPrice = "0." + nZero + nPrice;
              // add a zero and a dot before the percent value

              var nVal = Number(nPrice) * Number(nDiscount);
              event.target.value = Number(nPrice) - nVal;




              It would be easier if you could share an example or a mockup in PDF.
              Last edited by JRB; 02-24-2021, 04:54 PM.

              Comment


              • #8
                JRB
                THANKS... I attached a mockup as suggested. This particular client is for 15% discount but I want to be able to adjust that for other clients in the future. For now 15% is what I need. Also, I think I have an older version that does not have the option to add it so I can't put in a date selector on the left column, Can that be something added?

                Thanks!
                Attached Files

                Comment


                • #9
                  JACKIEJAY
                  Please click here to download the modified document below for a new form calculation, let me know if that meets your needs. Thanks!

                  Last edited by Lisa_lee; 03-02-2021, 08:06 AM.

                  Comment


                  • #10
                    Originally posted by Roy_Chen View Post
                    JACKIEJAY
                    Please click here to download the modified document
                    This document is OK but a Reset will permanently prevent the calculation.

                    It would be more reliable to assign "15" as the default value to the "Discount" field and rely on this value for the calculation.
                    For that in the script you have to replace "value" by "defaultValue".

                    And then to change the value of the discount, just change the default value of the "Discount" field… And Reset!

                    Last edited by JRB; 03-02-2021, 08:00 PM.

                    Comment


                    • #11
                      JRB
                      I got an error when I change "Value" to "defaultValue", which said that the value doesn't match the field format(see attached). And I have reset all values from the Total field, the calculation works fine without issue, I have no idea why you said a Reset will permanently prevent the calculation. How about you JACKIEJAY
                      Attached Files

                      Comment


                      • #12
                        Reset means: revert all form fields to their default value.
                        If you don't assign a default value to the "Discount" field, a reset will revert its value to nothing, therefore the following calculations will not work.

                        But I made a little mistake, since the "Discount" field have a default value, using value or defaultValue should make no difference for calculation.

                        Comment


                        • #13
                          JRB
                          You're right, and the new change on my file was I set a default value to 0.8 at Text Field Properties->Options, see attached, and that will keep the calculation working fine even resetting.
                          Attached Files

                          Comment


                          • #14
                            Thank you for all your help!

                            Comment

                            Working...
                            X