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

Add numbers in Javascript

Explorer ,
Feb 28, 2017 Feb 28, 2017

I have a series of numbers that I need to add, they are in 9 fields. The page is a template, the first problem was getting it change the names of the fields to match the spawned page within the script. I solved that problem.

The current problem is that if one of the fields is blank, it does not read that field as a '0', it stops adding and combines the rest of the numbers as if they were text.

So, if the first 3 fields were 1,1,2 the result would be '4', however if they were 1,1, (blank),2 I would get '22'. It adds the first 2 numbers, then it hits the blank, then it will combine the numbers thereafter.  1,1,(blank),2,3,4 would be 2234, I want it to = 11

I need it to consider a blank field a zero so that it continues to add, the 'custom calculation  script' is below, please help.

(function () {var f_name = event.target.name; var aName = f_name.split("."); var f_prefix = aName[0] + "." + aName[1];

event.value =

this.getField([f_prefix] + ".ADA A QTY").value

+

this.getField([f_prefix] + ".ADA B QTY").value

+

this.getField([f_prefix] + ".ADA C QTY").value

+

this.getField([f_prefix] + ".ADA D QTY").value

+

this.getField([f_prefix] + ".ADA E QTY").value

+

this.getField([f_prefix] + ".ADA F QTY").value

+

this.getField([f_prefix] + ".ADA G QTY").value

+

this.getField([f_prefix] + ".ADA H QTY").value

+

this.getField([f_prefix] + ".ADA I QTY").value

})();

TOPICS
Acrobat SDK and JavaScript , Windows
528
Translate
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

correct answers 1 Correct answer

Community Expert , Feb 28, 2017 Feb 28, 2017

You need to explicitly convert each value to a number. For example:

Number(this.getField(f_prefix + ".ADA A QTY").value)

I also removed the square brackets around f_prefix. They are not needed and can cause the script to fail.

Translate
Community Expert ,
Feb 28, 2017 Feb 28, 2017

You need to explicitly convert each value to a number. For example:

Number(this.getField(f_prefix + ".ADA A QTY").value)

I also removed the square brackets around f_prefix. They are not needed and can cause the script to fail.

Translate
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
Explorer ,
Mar 01, 2017 Mar 01, 2017
LATEST

That did the trick, thank you very much!

Translate
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