Skip to main content
leonardn93004688
Participant
December 20, 2016
Question

How do I enter the calculation F! xF2/C into the custom calculation script box of a text form field

  • December 20, 2016
  • 1 reply
  • 379 views

How do I enter the calculation F1 x F2/C into the custom calculation script box of a text form field

This topic has been closed for replies.

1 reply

Inspiring
December 20, 2016

Is F1, F2, and C are the field names involved, and you want the field to be blank when the denominator evaluates to zero, you could use something like:

// Custom calculation script

(function () {

    // Get the field values, as numbers

    var F1 = +getField("F1").value;

    var F2 = +getField("F2").value;

    var C = +getField("C").value;

    // Set this field's value

    if (C !== 0) {

        event.value = F1 * F2 / C;

    } else {

        event.value = "";

    }

})();

If you want to round the result, to the nearest one hundredth for example, that one line would be:

        event.value = util.printf("%.2f", F1 * F2 / C);

leonardn93004688
Participant
December 21, 2016

Sorry I should have been more specific. C is a constant (in this case 14.5) not a field and the denominator will never evaluate to zero. The result is required to one decimal place.. Please advise revised script.

leonardn93004688
Participant
December 29, 2016

As I had not received a reply to my last post I decided to modify your script as follows:-

(function () {

var F1=+getField("F1").value;

var F2=+getField("F2").value;

event.value = F1 * F2/14.5;

})();

To my surprise and delight it worked. The one decimal place was taken care of by the format tag in form field properties.Thanks for your partial help.