Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Average function?

  • 04-09-2006 10:50AM
    #1
    Registered Users, Registered Users 2 Posts: 224 ✭✭


    Hi
    I m currently creating an ASP page which Im using VB Script with. I need to come up with a way to calculate an average of an ever changing amount of fields. The total sum is made up of a max of 3 different figures depending on the item you choose.
    e.g. item1 is made up of a unitcost, printcost and freightcharge. Each cost is margined. so to say for arguements sake each cost had a % margin of 20%, then you would add 20+20+20 & divide by 3.
    Now Item2 might only have unitcost and freight charge, so Id be adding up only 2 margins and dividing by 2. and so on...

    Is there a way I can determine the amount of fields and average the margins depending on the amount of fields in the equation

    Any ideas


Comments

  • Registered Users, Registered Users 2 Posts: 4,188 ✭✭✭pH


    Just count the number of items and divide at the end:
    total = 0
    count = 0;
    avg = 0;
    for each item {
     total = total + margin
     count++
    }
    if (count > 0) {
     avg = total / count
    }
    

    However, adding the margin percentages together and dividing at the end will not give you a meaningful figure - average or otherwise. To calculate an average margin you need to add the actual amounts (in cents) together and divide by the total amount. Adding percentages together will give a meaningless result.


Advertisement