我有这段代码可以计算 datagridview 中所有行的 2 个相乘列的摘要。
private void vypocti_odvody(int price, int vat, int tot_rows2)
{
try
{
double outVal = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
outVal = outVal + (Convert.ToDouble(row.Cells[price].Value)/100) * Convert.ToDouble(row.Cells[vat].Value) + (Convert.ToDouble(row.Cells[price].Value));
}
// s_ub_celk.Text = (((a / 100) * b) + a).ToString();
Convert.ToDecimal ( result.Text = outVal.ToString()) ;
}
catch (Exception ex)
{
MessageBox.Show("Chybové hlášení K3 " + ex.Message.ToString());
}
}
如何改进小数位?现在结果是这样的:123,5484250 我希望结果只有 2 个小数位,例如 123,55。
抱歉我的英语不好,我希望“小数位”能表达我真正的意思。以防我举个例子以便更好地理解。
感谢大家的时间、评论和回答。
最佳答案
由于您使用的是 DataGridView
,因此您可以像这样设置单元格的格式
DataGridView1.Columns[required column index or name].DefaultCellStyle.Format = "N2"
关于c# - 结果 c# 中的小数位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17965771/