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

Need Javascript function help - division by zero.

Community Beginner ,
May 11, 2017 May 11, 2017

Hello,

I am new to Acrobat Javascript and I would like to get some help on this:

I have a pdf form where I have a Volume "VF" [liter] in one field and a Flow "Q" [liter/minute] in another field. In the third field I would like the calculation of Emptying time in seconds: (VF/Q)*60

Now, I manage to get the formula right, but as you start to fill in the other fields, the Q field is equal to zero so you get a message that the stated value do not match the field format. I understand this has something to do with the denominator is 0. I googled and tried the Javascript below but it doesnt work. What is wrong?

(all fields have the same format.)

// Custom calculation script(function () { // Get field values as numbersvar v1 = +getField("VF").value;var v2 = +getField("Q").value; // Perform calculation if denominator is not zero (or blank)if (v2 !== 0) {event.value = ( v1 / v2 ) * 60;} else {event.value = "";} })();

TOPICS
Acrobat SDK and JavaScript , Windows
1.6K
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

LEGEND , May 11, 2017 May 11, 2017

Try != not !== .  I didn't know !== even existed until just now And it doesn't seem quite right though it looks good in theory. If still no good, any message in console?

Translate
Community Expert ,
May 11, 2017 May 11, 2017

Don't put the code in one line.

// Custom calculation script

(function () {

// Get field values as numbers

var v1 = +getField("VF").value;

var v2 = +getField("Q").value;

// Perform calculation if denominator is not zero (or blank)

if (v2 != 0) {event.value = ( v1 / v2 ) * 60;} else {event.value = "";}

})();

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
LEGEND ,
May 11, 2017 May 11, 2017

Try != not !== .  I didn't know !== even existed until just now And it doesn't seem quite right though it looks good in theory. If still no good, any message in console?

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
Community Beginner ,
May 11, 2017 May 11, 2017

Thanks it works now!

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
Community Expert ,
May 11, 2017 May 11, 2017
LATEST

There's a really helpful explanation of comparison operators here...

Comparison operators - JavaScript | MDN

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