Skip to main content
neophyte1978
Participating Frequently
July 21, 2016
Question

need help changing excel formula to javascript for medication order form

  • July 21, 2016
  • 10 replies
  • 714 views

Hi, I'm trying to convert this excel formula to javascript:

=ROUND((0.007184*(E6)^0.725*(G6)^0.425),3-LEN(INT(0.007184*(E6)^0.725*(G6)^0.425)))

where E6 is a pdf field named Ht

           G6 is a pdf field named Wt

any help would be very much appreciated.

This topic has been closed for replies.

10 replies

Karl Heinz  Kremer
Community Expert
Community Expert
July 21, 2016

The problem with translating such an Excel formula is that the round function is different in JavaScript. I would solve the problem like this (the following assumes that there are never any negative results):

var E6 = this.getField("Ht").value;

var G6 = this.getField("Wt").value;

var v = Math.pow(0.007184 * E6, 0.725) * Math.pow( G6, 0.425);

if (v < 10.0) {

    // round to two decimal places

    event.value = Math.round(v * 100) / 100;

}

else if (v < 100.0) {

    // round to one decimal

    event.value = Math.round(v * 10) / 10;

}

else {

    // round to the nearest integer

    event.value = Math.round(v);

}

You would use this as your custom calculation script in your result field.

neophyte1978
Participating Frequently
July 23, 2016

Yes the rounding was where I had trouble. This worked perfectly! Thank you!

try67
Community Expert
Community Expert
July 21, 2016

All of the methods you need to use are described here:

JavaScript Math Reference

JavaScript String Reference

General tutorial about calculations in Acrobat: https://acrobatusers.com/tutorials/how-to-do-not-so-simple-form-calculations

neophyte1978
Participating Frequently
July 23, 2016

Very helpful indeed! Thank you!