在线测试编辑器:菜鸟编辑器
double a=545.925;
string b=Math.Round(a,2,MidpointRounding.AwayFromZero).ToString();
Console.WriteLine(b);//545.92
string b2=Math.Round((decimal)a, 2,MidpointRounding.AwayFromZero).ToString();
Console.WriteLine(b2);//545.93
string c=a.ToString("f2"); //强烈推荐
Console.WriteLine(c);//545.93
a=545.9251;
b=Math.Round(a,2,MidpointRounding.AwayFromZero).ToString();
Console.WriteLine(b);//545.93
a=545.995;
c=a.ToString("f2"); //强烈推荐
Console.WriteLine(c);//546.00
Console.ReadKey();
总结:四舍五入保留两位小数,同时还能补0占位的方法:a.ToString("f2"); 保留几位小数就f几。