繁体   English   中英

我如何计算平均值

[英]How do I calculate 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);
}

平均值是元素的总和除以元素总数。 在您的情况下,由于共有三个元素,因此您必须将总和除以三。

您不是在进行平均计算,而是在进行求和计算。 只需将总数除以提供的值数即可得出平均值:

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