• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Percentage calculation

Community Beginner ,
Jun 14, 2018 Jun 14, 2018

Copy link to clipboard

Copied

When formatting a field the 'percentage' category multiplies the field by 100. However, if this is not the calculation required, how do I "reset" the value or insert a script to ensure that I get the right percentage? For example, I want to show 7% of a sub-total but the form gives me a calculation of 700%. How to solve this?

TOPICS
PDF forms

Views

10.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
LEGEND ,
Jun 16, 2018 Jun 16, 2018

Copy link to clipboard

Copied

A picture of just the Format tab for a field does not provide a full picture.

Here is an image of some fields showing the formatted value, the actual value  of the field, and the string value of the field. You will note that the calculate actual value of the field is different from the formatted value of the field. The formatted value rounds the result of the calculation. Also you should note the far decimal value would not be expected for calculation. This is a result of how a computer calculates using binary values and the affect of the conversion from decimal values to binary values to perform the calculation. So if one is  performing financial calculations, including tax calculations, one should round the actual value of the field or the small values in the raw calculation will accumulate which can result in a wrong answer.

FormattingAffect.jpg

Sample Formatting Effects is a link to the above file for your inspection.

Edit to add:

The left column is the input column, the middle column is the value of the input field used by JavaScript, and the final column is the actual user inputted value.

As one can see the displayed value for an inputted number the display includes the thousands separator and decimal place along with suppressing and leading zeros the user may have inputted. When JavaScript accesses the value the leading zeros are dropped as well as the thousands separator(s). And if one used the "valueAsStirng" property, JavaScript will return the leading zeros. When one looks at the "Percentage" the value is displayed with the "%" symbol and has shifted the decimal point of the inputted value to the right 2 decimal places. This approach for the "Percentage" format allows the use of the value for the computation using a percentage to be used without any modification of the inputted or computed value.

When one looks at the calculated row the computed value in the middle column shows the computation is carried out to more decimal places than is displayed and the  displayed value is rounded. If one were to use this value in another calculation, the unrounded value would be used. This may or may not be an acceptable solution to the final calculated result on the form. For statistical reporting this may be the best solution, but for financial calculations it can introduce an error in the final result because of the additional fractional cents being included in the summation. Also note that there is an addition very small value tacked on the end of the value. This is caused by the fact that computers perform calculation using binary values and the conversion from a human readable decimal value to a binary value does not always result in a rational binary value.  As a result of all of this, form developers must consider these issues when creating calculation scripts.

When one looks at the phone number one will see the phone number was entered into the form without any separators but the displayed value includes the standard North American separators for the Area Code and Exchange values.

The date and time calculation is an example of how the formatted result in hours and minutes is not the same as the calculated value in minutes only.

Also one should be aware that for numbers that may include leading zeros like U.S. Postal Zip Codes and the U.S. Social Security number the inputted leading zeros are accessible to JavaScript by using the "valueAsString" property.

View solution in original post

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 14, 2018 Jun 14, 2018

Copy link to clipboard

Copied

What calculation do you use?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

Hi Bernd

Thanks for getting back to me. I used the commands provided by Acrobat, i.e., when formatting text field properties I specified 'Percentage', which as you know multiplies the field value by 100 and displays a '%'.

However, I have since solved the problem by inserting javascript commands to divide the calculation by 100, as follows:

var a=this.getField("SUB-TOTAL");

var b=this.getField("GST RATE");

var c=this.getField("GST");

c.value=(a.value*b.value)/100);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

You should be aware that the "Format" tab setting only affects the displayed value and not the actual value of the field used by JavaScript.

In what field did you place your code?

// custom calculation for the GST field;

var a = this.getField("SUB-TOTAL");

var b = this.getField("GST RATE");

event.value = a.value * b.value;

The "SUB-TOTAL" field has a format of "Number" and decimal places as needed.

The "GST-RATE" field has a format of "Percentage", decimal places as needed, a default value of ").07", and is set as read only.

The field "GST" field has a format of "Number", decimal places as needed, and is set to read only.

This calculation could be done in any of the three calculation methods but some special coding would need to be added in the "Simplified Field notation" option to adjust for the field names containing an arithmetic operator within its name.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

Hi gkaiseril

I don't understand what you mean by ""Format" tab setting only affects the displayed value and not the actual value of the field used by JavaScript". How can the displayed value not be the actual value of the field?

Anyway, to answer your question, the code is placed in the GST RATE field (where the percentage amount is a variable) and I corrected it to read:

var a=this.getField("SUB-TOTAL");

var b=this.getField("GST RATE");

var c=this.getField("GST");

c.value=(a.value*b.value)/100;

This calculates and displays the correct amount of tax to be added to the sub-total to get the grand total. Thanks for taking the trouble to provide your input.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

If you use JavaScript to show the "GST RATE" field value you will get a value of .07 not "7 %" as displayed in the form field.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 15, 2018 Jun 15, 2018

Copy link to clipboard

Copied

I wish that were true. Try the script out in Acrobat and you'll see it does what I need. And note:

window.jpg

See the text next to the bulb. That's what happens, which I don't want in my form. Thanks again.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 16, 2018 Jun 16, 2018

Copy link to clipboard

Copied

A picture of just the Format tab for a field does not provide a full picture.

Here is an image of some fields showing the formatted value, the actual value  of the field, and the string value of the field. You will note that the calculate actual value of the field is different from the formatted value of the field. The formatted value rounds the result of the calculation. Also you should note the far decimal value would not be expected for calculation. This is a result of how a computer calculates using binary values and the affect of the conversion from decimal values to binary values to perform the calculation. So if one is  performing financial calculations, including tax calculations, one should round the actual value of the field or the small values in the raw calculation will accumulate which can result in a wrong answer.

FormattingAffect.jpg

Sample Formatting Effects is a link to the above file for your inspection.

Edit to add:

The left column is the input column, the middle column is the value of the input field used by JavaScript, and the final column is the actual user inputted value.

As one can see the displayed value for an inputted number the display includes the thousands separator and decimal place along with suppressing and leading zeros the user may have inputted. When JavaScript accesses the value the leading zeros are dropped as well as the thousands separator(s). And if one used the "valueAsStirng" property, JavaScript will return the leading zeros. When one looks at the "Percentage" the value is displayed with the "%" symbol and has shifted the decimal point of the inputted value to the right 2 decimal places. This approach for the "Percentage" format allows the use of the value for the computation using a percentage to be used without any modification of the inputted or computed value.

When one looks at the calculated row the computed value in the middle column shows the computation is carried out to more decimal places than is displayed and the  displayed value is rounded. If one were to use this value in another calculation, the unrounded value would be used. This may or may not be an acceptable solution to the final calculated result on the form. For statistical reporting this may be the best solution, but for financial calculations it can introduce an error in the final result because of the additional fractional cents being included in the summation. Also note that there is an addition very small value tacked on the end of the value. This is caused by the fact that computers perform calculation using binary values and the conversion from a human readable decimal value to a binary value does not always result in a rational binary value.  As a result of all of this, form developers must consider these issues when creating calculation scripts.

When one looks at the phone number one will see the phone number was entered into the form without any separators but the displayed value includes the standard North American separators for the Area Code and Exchange values.

The date and time calculation is an example of how the formatted result in hours and minutes is not the same as the calculated value in minutes only.

Also one should be aware that for numbers that may include leading zeros like U.S. Postal Zip Codes and the U.S. Social Security number the inputted leading zeros are accessible to JavaScript by using the "valueAsString" property.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 16, 2018 Jun 16, 2018

Copy link to clipboard

Copied

When you don't want this then use the number format.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 16, 2018 Jun 16, 2018

Copy link to clipboard

Copied

If you are doing a large number of this type of calculation the unrounded value can introduce a calculation error especially in financial calculatins. Think tax forms, expense reports, mileage reimbursement, etc.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 14, 2025 Jan 14, 2025

Copy link to clipboard

Copied

LATEST
 

You could try adjusting the calculation script to divide the percentage value by 100 before displaying it in the field. This helps avoid the multiplication error you're seeing.

 

If you're working with academic percentages, I also use an online CGPA to Percentage Calculator that provides accurate results instantly.  it simplifies the process and ensures precise conversions.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines