Skip to main content
marie@fogmaker
Participating Frequently
May 11, 2017
Answered

Need Javascript function help - division by zero.

  • May 11, 2017
  • 2 replies
  • 1822 views

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 = "";} })();

This topic has been closed for replies.
Correct answer Test Screen Name

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?

2 replies

Test Screen NameCorrect answer
Legend
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?

marie@fogmaker
Participating Frequently
May 11, 2017

Thanks it works now!

Bernd Alheit
Community Expert
Community Expert
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 = "";}

})();