当前位置: 移动技术网 > IT编程>开发语言>c# > C#使用round函数四舍五入的方法

C#使用round函数四舍五入的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#使用round函数四舍五入的方法。分享给大家供大家参考。具体分析如下:

c#中的round函数实际上不是真正的四舍五入函数,一般的程序设计语言的round函数也都不是四舍五入函数,而是银行家舍入法函数,也就是“四舍六入五考虑,五后非零就进一,五后为零看奇偶,五前为偶应舍去,五前为奇要进一”

但c#中的round函数似乎也没有完全遵循这个规则,我们来看看微软官方给的范例:

using system;
public class example
{
  public static void main()
  {
   double[] values = { 2.125, 2.135, 2.145, 3.125, 3.135, 3.145 };
   foreach (double value in values)
     console.writeline("{0} --> {1}", value,
  math.round(value, 2, midpointrounding.awayfromzero));
  }
}
// the example displays the following output:
//    2.125 --> 2.13
//    2.135 --> 2.13
//    2.145 --> 2.15
//    3.125 --> 3.13
//    3.135 --> 3.14
//    3.145 --> 3.15

看到了吧,2.135和3.135 做了round操作后得到的结果居然是2.135不进位,2.145进位了。

希望本文所述对大家的c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网