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

Field format and rounding

New Here ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

Hello there, I'm trying my hand at what seems like a simple calculation. I got things to work from searching google as I do not know javascript. I can send the form if needed but this is what I have as far as numbers.  I can also share the codes I've put in so far. 

 

Ln 5 (Amount2) is a number entered by the user (format as number no digits). 

Ln 6 (Amount3) is a number entered by user (Same as above)

Ln 7 (Amount4) is code to subtract Ln 6 from Ln 5. So if nothing is in Ln 6 then it basically carries it down. 

 

Ln 8 (Perc1) is a % entered by user (formated as number to no digits) either 12 or 09 percent

Ln 9 (Amount5) is the Ln7 times Ln8.  

var Amount4 = getField("Amount4").value;

var Perc1 = getField("Perc1").value;

var Percentage = Perc1 / 100;

event.value = Amount4 * Percentage;

 

Ln9 (Amount5) is no decimals but I realize it needs formatted or a custom format to make it read a rounded whole number for the other fields 

Ln10 (Perc2) is a %  entered by user between 0 & 100 percent. Formatted as number with 1 decimal

Ln11 (Amount6) is the calculation for Ln9 times Ln10 and is rounded to the nearest dollar. 

var Amount5 = getField("Amount5").value;
var Perc2 = getField("Perc2").value;
ar Percentage = Perc2 / 100;
event.value = Amount5 * Percentage;

 

I can't get line 9 and line 11 to round to the nearest dollar when calculated. I tried using 

event.value = Math.round(Amount6) at the end of the above script. 

 

If Ln9 calculates as 169.83 it should show as 170 and then Ln 10 would multiply that by 75% making Ln11 127.5 rounded to 128. 

If Ln11 calculated to 127.4 then I need it to round down to 127. 

I know part is the way the Ln9 is formatted. I think I need a specific format.  

 

If it helps this is a tax form. 

H_0-1674933078881.png

 

TOPICS
JavaScript , PDF forms

Views

382

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 ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

You have to be careful with the calculations and formatting. 

The field format script does not change the value of the field. It changes how the field is displayed to the user. The value remains the same no matter what is displayed. 

So if you want the field value to be rounded, then the rounding has to be part of the calculation.  

 

For example, the calculation for Line9 should look like this

 

var Amount4 = getField("Amount4").value;
var Perc1 = getField("Perc1").value;
var Percentage = Perc1 / 100;
event.value = Math.round(Amount4 * Percentage);

 

 

See this article for more information:

https://www.pdfscripting.com/public/Formatting-Form-Fields.cfm

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

LATEST

Great, thank you I will look at those and reply if I have issues. 🙂

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