c# - 结果 c# 中的小数位

标签 c# math datagridview double

我有这段代码可以计算 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/

相关文章:

c# - DataGridView 自动调整大小但限制最大列大小

c# - 是否有一种有效的手写文本分割算法?

c# - 对于在后台运行且没有 UI 的 C# 程序,我应该使用什么类型的项目?

algorithm - 将任何一个数字减少到 0 的最佳方法

c# - 从状态向量中找到真正的异常

sql - ds.Tables.Rows.Add() 调用一次时生成 3 行

c# - 数据 GridView 中的数据不会直观地显示 C# SQL

c# - 按顺序获取文件中的行的最佳方法?

javascript - 从根本上来说,C# 和 Javascript 中的 "this"关键字是否相同

arrays - 查找数组中的连接组件