c# - 转换为小数点后两位

标签 c# decimal rounding

您好,我是一名 C# 新手,有人会礼貌地告诉我如何将这段代码的值转换为 double /四舍五入的小数。提前致谢

DataTable dtValues = new DataTable("GetValues");

strValueNumber = ValueNumber[0].ToString();
dtGetValues = SQLMethods.GetValues(strValueNumber);

total = 0;

for (int i = 0; i < dtValues.Rows.Count; i++)
{
    total1 = total1 + Convert.ToInt32(dtGetValues.Rows[i]["total_1"]);                  
    total2 = total2 + Convert.ToDouble(dtGetValues.Rows[i]["total_2l"]) * .45; 

    tbtotal1.Text = total1.ToString();
    tbtotal2.Text = total2.ToString(); 
}
}               
catch (Exception ex)
{
    MessageBox.Show("Error in returning selected Values. " +
                    "Processed with error:" + ex.Message);
}
}

最佳答案

使用Math.Round

Math.Round(mydoublevalue, 2);

在你的代码中

tbtotal2.Text = Math.Round(total2, 2).ToString(); 

关于c# - 转换为小数点后两位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11213052/

相关文章:

python 小数 - 四舍五入到最接近的整数美元(无美分) - 使用 ROUND_HALF_UP

objective-c - Objective C 奇怪的十进制到 long long 的转换

python - 如何防止 csv.DictWriter() 或 writerow() 舍入我的 float ?

c# - 将 Math.Round 结果转换为 float 是否安全?

sql - 当不需要舍入时使用 LIKE 来匹配 float

c# - Moq 中的 SetupSequence

c# - 如何使用System.Net.Http修补数据

c# - 填充可为空的小数时,小数会四舍五入,为什么?

c# - 使用 C# 读取发布数据

c# - 如何隐藏 ListView 控件中的列?