Skip to main content
jasons35699793
Known Participant
April 2, 2019
Question

Placing decimals and whole numbers in separate boxes

  • April 2, 2019
  • 2 replies
  • 676 views

I have a form that separates whole numbers and decimals. What I need to do it take the sum of two boxes 1stFloor and 1stFloordec subtract 0.4 from the total and place the whole number in Garage and the remaining decimal in Garagedec. Summing the two and subtracting works fine its just getting it to place the whole number in Garage and the remaining decimal in Garagedec I'm having trouble with.

This topic has been closed for replies.

2 replies

jasons35699793
Known Participant
April 3, 2019

Here is what I have so far apparently my formula is wrong.

var num = this.getField("HAG").value + this.getField("HAGdec/10").value + 2;

varstrNum = util.printf("%2.2f",num);

var aNum = strNum.split(".");

event.value = aNum[0];

this.getField("FirstFloordec").value = aNum[1];

I'm trying to take the value in HAG and add the value in HAGdec to is divided by 10 then add 2 to it. Once I have that I want to take the outcome and split it between the integer and the decimal and place the integer in FirstFloor and the decimal in FirstFloordec.

Thom Parker
Community Expert
Community Expert
April 3, 2019

So why do you think this is wrong?  Is the console window reporting an error? The console window is invaluable for developing scripts in Acrobat. Here's an old tutorial on it (some of the UI has changed, but everything else is the same).

Free Video Content & Full Listing of Available Videos

There is one obvious syntax error in the code. This line needs a space after "var"

var strNum = util.printf("%2.2f",num);

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
jasons35699793
Known Participant
April 3, 2019

What is happening is in the place for FirstFloor it places a 1 and in FirstFloordec it is showing #R so i'm guessing it must be something with my formula

Thom Parker
Community Expert
Community Expert
April 2, 2019

The easy solution to convert the number to formatted text, then split on the decimal.

var num = ... calculation to get number ...

var strNum = util.printf("%2.2f",num);

var aNum = strNum.split(".");

this.getField("Garage").value = aNum[0];

this.getField("Garageded").value = aNum[1];

Read more about manipulating string here:

https://acrobatusers.com/tutorials/splitting-and-rebuilding-strings

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
jasons35699793
Known Participant
April 2, 2019

I have very little experience with Javascript and even less with acrobat. On my form for the file Garage would I place the code in the calculations box or elsewhere?

Thom Parker
Community Expert
Community Expert
April 2, 2019

The code I provided is a generic outline, cause I don't know the specifics of your form, but a calculation script is a good location.

Lets say that the script will be placed in the "Garage" field. Here's how you would write it.

var num = this.getField("1stFloor").value + this.getField("1stFloordec").value  - 0.4;

var strNum = util.printf("%2.2f",num);

var aNum = strNum.split(".");

event.value = aNum[0];

this.getField("Garageded").value = aNum[1];

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