关于C#里面的Math.Round,很多人都会用到,而且以为是四舍五入,其实不是这样的:
C#里面的Math.Round是符合IEEE标准的“四舍五入”,其实是五舍六入。看下面测试:
double testd = 3184.39995117187;
double nd = Math.Round(testd, 2); // 3184.4
nd = Math.Round(testd, 2, MidpointRounding.AwayFromZero);// 3184.4
nd = Math.Round(3184.367, 2); // 3184.37
nd = Math.Round(3184.365, 2); // 3184.36
nd = Math.Round(3184.367, 2, MidpointRounding.AwayFromZero); //3184.37
nd = Math.Round(3184.365, 2, MidpointRounding.AwayFromZero); //3184.37
如果想变成我们理解的四舍五入,使用这个属性即可。
MidpointRounding.AwayFromZero