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

Placing decimals and whole numbers in separate boxes

New Here ,
Apr 02, 2019 Apr 02, 2019

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript

Views

169

Likes

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 ,
Apr 02, 2019 Apr 02, 2019

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

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 ,
Apr 02, 2019 Apr 02, 2019

Copy link to clipboard

Copied

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?

Likes

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 ,
Apr 02, 2019 Apr 02, 2019

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

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 ,
Apr 03, 2019 Apr 03, 2019

Copy link to clipboard

Copied

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.

Likes

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 ,
Apr 03, 2019 Apr 03, 2019

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

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 ,
Apr 03, 2019 Apr 03, 2019

Copy link to clipboard

Copied

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

Likes

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 ,
Apr 03, 2019 Apr 03, 2019

Copy link to clipboard

Copied

So, is "HAGdec/10" the name of an actual field? Or is this intended to divide the field value by 10?

If it is for the division then, that is the issue, cause it won't work.

So did you look in the console window to see if an error is reported? This is the only way you'll know if there was an error, and what it is.

You really need to get the basic math to work first, before trying to split it up. So work on that first.

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

Likes

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 ,
Apr 03, 2019 Apr 03, 2019

Copy link to clipboard

Copied

I don't seem to have the option for Javascript in my acrobat

Likes

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 ,
Apr 03, 2019 Apr 03, 2019

Copy link to clipboard

Copied

Ok I got the console window working but for the first line of code it keeps saying undefined

Likes

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 ,
Apr 03, 2019 Apr 03, 2019

Copy link to clipboard

Copied

Thank you the console window helped! I managed to get it returning the proper value the only issue I have is that in the place where the decimal goes it is using 2 digits and I only need one.

Likes

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 ,
Apr 03, 2019 Apr 03, 2019

Copy link to clipboard

Copied

LATEST

The string conversion code specifies 2 decimals.

Try this code:

util.printf("%0.1f",num);

Look up "util.printf" to get the complete list of formatting options.

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

Likes

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