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

Syntax for checking a numeric field for a value

Engaged ,
Oct 11, 2018 Oct 11, 2018

Copy link to clipboard

Copied

I've used both of these approaches successfully but would like to know which is the preferred way to add numeric field values?

     a. Is example 2 preferred to example 1, or 1 preferred to 2?

     b. When adding a series of numbers as in these examples, is it always preferable to enclose the number variables in parenthesis?

     c. In example 1, is using "this.getField" preferred over "getField"

     d. Is there a better way to do this?

     e. Does either of these approaches encourage concatenating values vs. totaling their values, so that 1 + 2 + 3 + 4 = 1234 instead of = 10?

Example 1

var m01 = getField("num.CD.M.01");

var m02 = getField("num.CD.M.02");

var m03 = getField("num.CD.M.03");

var m04 = getField("num.CD.M.04");

var creditsTotal = this.getField("num.CreditsTotal");

     creditsTotal.value = m01.value + m02.value + m03.value + m04.value;

Example 2

var m01 = +getField("num.CD.M.01").value;

var m02 = +getField("num.CD.M.02").value;

var m03 = +getField("num.CD.M.03").value;

var m04 = +getField("num.CD.M.04").value;

var creditsTotal = this.getField("num.Credits");

     creditsTotal.value = (m01 + m02 + m03 + m04);

TOPICS
Acrobat SDK and JavaScript

Views

368

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

correct answers 1 Correct answer

LEGEND , Oct 11, 2018 Oct 11, 2018

When you are doing numerical addition involving field values, you should always explicitly convert the value to a number, like your second example does. If you're using a script like this in a field's calculate event, it should be something like:

var m01 = +getField("num.CD.M.01").value;

var m02 = +getField("num.CD.M.02").value;

var m03 = +getField("num.CD.M.03").value;

var m04 = +getField("num.CD.M.04").value;

event.value = m01 + m02 + m03 + m04;

Also, JavaScript doesn't guess or assume things when i

...

Votes

Translate

Translate
LEGEND ,
Oct 11, 2018 Oct 11, 2018

Copy link to clipboard

Copied

First, a field resides within a PDF file. Acrobat can have more than one file open at a time and one PDF can open another PDF file. The issue then becomes identifying the PDF file in which the field exist or which file the field is to be accessed when both files have the same field name. The "this" is the default PDF file object when a PDF is opened by double clicking on the file or using the File => Open commands. If one uses JavaScript to open another PDF file from within is PDF is is best to provide an object name for that PDF.

Next the "+" operator can concatenate two string fields or add two numeric values. Unfortunately numeric values can be mistaken for character strings and JavaScript may make an "educated" guess as to whether a number character is to be treated either as a string character or a number. For the subtraction, multiplication, and division operations all values are considered to be numbers. Since the "+" operation can process values as either string values or numeric values what happens is dependent upon what JavaScript assumes the value to be. It is best to use a numeric constrictor like "Number()", multiply by 1 or use the "+" immediately before the value.

Parenthesis are used to force the order of the mathematical  operations.

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 ,
Oct 11, 2018 Oct 11, 2018

Copy link to clipboard

Copied

LATEST

When you are doing numerical addition involving field values, you should always explicitly convert the value to a number, like your second example does. If you're using a script like this in a field's calculate event, it should be something like:

var m01 = +getField("num.CD.M.01").value;

var m02 = +getField("num.CD.M.02").value;

var m03 = +getField("num.CD.M.03").value;

var m04 = +getField("num.CD.M.04").value;

event.value = m01 + m02 + m03 + m04;

Also, JavaScript doesn't guess or assume things when it performs type coercion, it follows well defined rules (if sometimes goofy). An empty string, which is what the field value property will return, will evaluate to zero when converted. Some examples:

0 + "" = "0"

1 + 2 = 3

1 + 2 + "" = "3"

1 + "" + 2 = "12"

"" + 1 + 2 = "12"

+"" + 1 + 2 = 3

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