# stupid (easy) question. how do you do avg calc fields?

**URL:** <https://forum.servoy.com/t/stupid-easy-question-how-do-you-do-avg-calc-fields/4004>\
**Category:** Classic Servoy\
**Created:** [April 8, 2005, 9:11pm UTC](https://forum.servoy.com/t/stupid-easy-question-how-do-you-do-avg-calc-fields/4004 "2005-04-08T21:11:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sammyzheng](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sammyzheng](https://forum.servoy.com/u/sammyzheng)\
**Post date:** [April 8, 2005, 9:11pm UTC](https://forum.servoy.com/t/stupid-easy-question-how-do-you-do-avg-calc-fields/4004/1 "2005-04-08T21:11:46Z")

</div>

I can’t find this in documentation anywhere.

I have 5 fields. Weight 1, 2,3,4,5 . I want a stored calculation field that caculates the average of them.

I tried to define my calucation field "return avg(weight1,weight2,weight3…5)

But it does not work.

The only way i work around it now is "return (weight 1+2..5)/5  
which doesn’t work well because if i only enter a value into weight 1, it assumes wieght2 thru weight 5 field are 0 and returns a wrong average.

PLEASE HELP!

---

<div class="post-metadata">

**Author:** ![airmark](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@airmark](https://forum.servoy.com/u/airmark)\
**Post date:** [April 8, 2005, 9:39pm UTC](https://forum.servoy.com/t/stupid-easy-question-how-do-you-do-avg-calc-fields/4004/2 "2005-04-08T21:39:15Z")

</div>

Sammy,

Try this:

```auto
var ttlWeight = weight_1 + weight_2 + weight_3 + weight_4 + weight_5
var ttlFields = 0
if (weight_1 > 0){ttlFields += 1}
if (weight_2 > 0){ttlFields += 1}
if (weight_3 > 0){ttlFields += 1}
if (weight_4 > 0){ttlFields += 1}
if (weight_5 > 0){ttlFields += 1}
average_weight_field = ttlWeight / ttlFields

```

---

<div class="post-metadata">

**Author:** ![bcusick](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@bcusick](https://forum.servoy.com/u/bcusick)\
**Post date:** [April 8, 2005, 9:48pm UTC](https://forum.servoy.com/t/stupid-easy-question-how-do-you-do-avg-calc-fields/4004/3 "2005-04-08T21:48:30Z")

</div>

Hi Sammy,

No problem - here’s the code that I used (I used globals - but just substitute your field names). Also, make sure the return result is NUMBER - not integer (unless that’s what you want):

```auto
var count = 0
var total = 0

if(globals.num1)
{
	total += globals.num1
	count += 1
}

if(globals.num2)
{
	total += globals.num2
	count += 1
}

if(globals.num3)
{
	total += globals.num3
	count += 1
}

if(globals.num4)
{
	total += globals.num4
	count += 1
}

if(globals.num5)
{
	total += globals.num5
	count += 1
}

return total/count

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![airmark](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@airmark](https://forum.servoy.com/u/airmark)\
**Post date:** [April 8, 2005, 9:55pm UTC](https://forum.servoy.com/t/stupid-easy-question-how-do-you-do-avg-calc-fields/4004/4 "2005-04-08T21:55:07Z")

</div>

Sammy,

Sorry about that. You did say a calculation field! Last line should read

return ttlWeight/ttlFields

---

<div class="post-metadata">

**Author:** ![sammyzheng](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@sammyzheng](https://forum.servoy.com/u/sammyzheng)\
**Post date:** [April 11, 2005, 9:53pm UTC](https://forum.servoy.com/t/stupid-easy-question-how-do-you-do-avg-calc-fields/4004/5 "2005-04-11T21:53:38Z")

</div>

> bcusick:  
> Hi Sammy,
> 
> No problem - here’s the code that I used (I used globals - but just substitute your field names). Also, make sure the return result is NUMBER - not integer (unless that’s what you want):
> 
> ```auto
> var count = 0
> 
> ```

return total/count

> ```auto
> 
> Hope this helps.
> 
> ```

THANKS Bob and airmark,  
this works great. Just weird to see how servoy doesn’t have a built in average function.
