Skip navigation
Emaline83
Currently Being Moderated

Division Help

May 24, 2012 10:13 AM

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

 
Replies
  • George Johnson
    11,671 posts
    Aug 11, 2002
    Currently Being Moderated
    May 24, 2012 10:40 AM   in reply to Emaline83

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    May 24, 2012 11:34 AM   in reply to Emaline83

    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.

     
    |
    Mark as:
  • George Johnson
    11,671 posts
    Aug 11, 2002
    Currently Being Moderated
    May 24, 2012 12:57 PM   in reply to Emaline83

    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);

     
    |
    Mark as:
  • George Johnson
    11,671 posts
    Aug 11, 2002
    Currently Being Moderated
    May 24, 2012 1:06 PM   in reply to Emaline83

    For that you can do either of the following:

     

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

     

     

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

     
    |
    Mark as:
  • George Johnson
    11,671 posts
    Aug 11, 2002
    Currently Being Moderated
    May 24, 2012 1:12 PM   in reply to Emaline83

    Can you post the complete script you're using?

     
    |
    Mark as:
  • George Johnson
    11,671 posts
    Aug 11, 2002
    Currently Being Moderated
    May 24, 2012 1:15 PM   in reply to Emaline83

    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 = "";

        }

     

    })();

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points