Skip to main content
neophyte1978
Participating Frequently
Jul 21, 2016 7:46 PM UTC
Question

need help changing excel formula to javascript for medication order form

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 is closed to new replies. Start a new post to keep the conversation going.

10 replies

Karl Heinz  Kremer
Community Expert
Community Expert
Jul 21, 2016 8:54 PM UTC

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.

Translate
neophyte1978
Participating Frequently
Jul 23, 2016 1:51 AM UTC

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

Translate
try67
Community Expert
Community Expert
Jul 21, 2016 8:50 PM UTC

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

Translate
neophyte1978
Participating Frequently
Jul 23, 2016 1:49 AM UTC

Very helpful indeed! Thank you!

Translate