11 Replies Latest reply on May 24, 2012 1:18 PM by Emaline83

# Division Help

I know this has been on here before, but I cannot seem to get any of the answers to work for me.  This is greatly due to the fact that I do not know anything about JavaScript.

I am working on a form for my company and it has been requested that I set up one of the fields to automatically equal the division of two other fields.  What I need is the simple notation or the custom calculation script to make this happen.

It needs to do the following calculation:

ACTUALPOINTS / POSSIBLESCORE = FINAL SCORE

"ACTUALPOINTS" is field 1.  "POSSIBLESCORE" is field 2.  "FINAL SCORE" should = field 1 divided by field 2.

Any help would be greatly appreciated!!!!

- JavaScript Dumb

• ###### 1. Re: Division Help

Try this as the custom calculation script for the FINALSCORE field:

(function () {

// Get the field values, as numbers

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

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

// Set this field's value

if (v2 !== 0) {

event.value = v1 / v2;

} else {

// Set to blank if denominator evaluates to zero

event.value = "";

}

})();

Make sure the field calculation order is correct, and calculated fields should normally be set to read-only so the user is not able to interact with them.

• ###### 2. Re: Division Help

You can not divide by zero in JavaScript. So you have to test for a zero divisor before performing any division and properly control the execution of your JavaScript calculation.

• ###### 3. Re: Division Help

This worked beautifully! Thanks, is there any way to have it automatically round the results?

• ###### 4. Re: Division Help

You can add code to round the result. Exactly what would you like to do? The following can be used to round to the nearest hundredth. Just replace the other line with this one:

event.value = util.printf("%.2f", v1 / v2);

• ###### 5. Re: Division Help

Well, with the current (original) script in place:

Final Score is coming out to be 97.39130434782….

I would like it to round to 97 in this case.

• ###### 6. Re: Division Help

For that you can do either of the following:

event.value = util.printf("%.0f", v1 / v2);

event.value = Math.round(v1 / v2);

• ###### 7. Re: Division Help

Tried both, and it is still not rounding

• ###### 8. Re: Division Help

Can you post the complete script you're using?

• ###### 9. Re: Division Help

(function () {

// Get the field values, as numbers

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

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

// Set this field's value

if (v2 !== 0) {

event.value = v1 / v2*100;

} else {

// Set to blank if denominator evaluates to zero

event.value = util.printf("%.0f", v1 / v2);

}

})();

• ###### 10. Re: Division Help

This is what I meant:

(function () {

// Get the field values, as numbers

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

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

// Set this field's value

if (v2 !== 0) {

event.value = util.printf("%.0f", v1 / v2);

} else {

// Set to blank if denominator evaluates to zero

event.value = "";

}

})();

• ###### 11. Re: Division Help

That works! Thanks