简体   繁体   English

我如何计算平均值

[英]How do I calculate the average

Im able to get the sum in dataGridView but having trouble on properly getting the average.我能够在 dataGridView 中获得总和,但无法正确获得平均值。

foreach (DataGridViewRow row in dataGridView.Rows) {
    row.Cells["Average"].Value = Convert.ToDouble(row.Cells[4].Value) +
    Convert.ToDouble(row.Cells[5].Value) +
    Convert.ToDouble(row.Cells[6].Value);
}

The average is the sum of the elements divided by the total number of elements.平均值是元素的总和除以元素总数。 In your case, since there are total of three elements, you'll have to divide the sum by three.在您的情况下,由于共有三个元素,因此您必须将总和除以三。

You are not doing an average calculation but a sum calculation.您不是在进行平均计算,而是在进行求和计算。 Simply divide the total by the number of values provided to find the average:只需将总数除以提供的值数即可得出平均值:

foreach (DataGridViewRow row in dataGridView.Rows)
                    row.Cells["Average"].Value = (Convert.ToDouble(row.Cells[4].Value) + Convert.ToDouble(row.Cells[5].Value) + Convert.ToDouble(row.Cells[6].Value)) / 3;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM