8 Replies Latest reply on Oct 5, 2015 8:01 AM by gkaiseril

# How do I Average two fields and round down?

I need to average two fields and round down to the nearest whole number. The two fields will always each have a value.

If someone could help me out, I'd appreciate it.

T.

• ###### 1. Re: How do I Average two fields and round down?

Assuming you want this to be a custom calcuation script for a field, it could look something like this:

// Custom calculation script

(function () {

// Get the field values, as numbers

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

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

// Set this field's value to the average, rounded down

event.value = Math.floor((v1 + v2) / 2);

})();

More information on the Math.floor method can be found in any decent JavaScript reference.

• ###### 2. Re: How do I Average two fields and round down?

Thanks, George! Worked like a charm.

• ###### 3. Re: How do I Average two fields and round down?

How would you get this to ignore fields with a value of zero?

• ###### 4. Re: How do I Average two fields and round down?

Do you mean fields that are blank, have entries that are equal to zero (e.g., 0, 0.00, etc.), or both?

• ###### 5. Re: How do I Average two fields and round down?

George, thanks for the reply! I need to ignore fields that are less than 1. I don't have any blank fields.

• ###### 6. Re: How do I Average two fields and round down?

OK, do you just have two fields to average like the original question, or more than that? What should the result of the calculation be if all of the field values are less than 1?

• ###### 7. Re: How do I Average two fields and round down?

There are 15 sections that need to be averaged (all with a varying number of fields))

If all entries were "1" or "0", then the average should be "1". In the scenario in the inserted pic, the average should be 1.66 (1+1+3)/3.

• ###### 8. Re: How do I Average two fields and round down?

I would use a function to calculate the average form an array of the acceptable values. In this way I can reuse the code for summing, counting, and computing the average with the needed values in an array.