Math.Round(x)通过程序测试了几个个数据,列表如下:x值    反回值    正常值(中国人会计习惯)
0.5     0         1
-0.5     0        -1
0.6      1         1
-0.6     -1        -1查了点资料,C#的Round是四舍六入五取偶查到了点网上转载多的代码:
double ChinaRound(double value, int decimals)
{
if (value < 0)
{
return Math.Round(value + 5 / Math.Pow(10, decimals + 1), decimals, MidpointRounding.AwayFromZero);
}
else
{
return Math.Round(value, decimals, MidpointRounding.AwayFromZero);
}

//这断代码处理不了-0.5四舍五入到整数,已测试       public static double Round(double d, int i)
        {
            if (d >= 0)
            {
                d += 5 * Math.Pow(10, -(i + 1));
            }
            else
            {
                d += -5 * Math.Pow(10, -(i + 1));
            }
            string str = d.ToString();
            string[] strs = str.Split('.');
            int idot = str.IndexOf('.');
            string prestr = strs[0];
            string poststr = strs[1];
            if (poststr.Length > i)
            {
                poststr = str.Substring(idot + 1, i);
            }
            string strd = prestr + "." + poststr;
            d = Double.Parse(strd);
            return d;
        }
//这断代码处理-0.5四舍五入到整数会报异常,已测试

解决方案 »

  1.   

    没这么复杂把
      public static int GetInt(double a)
            {
                double _Value = 0;
                if (a == 0) return 0;
                if (a > 0)
                {
                    _Value =a+ 0.9999999999999999;
                }
                else
                {
                    _Value =a+ (-0.999999999999999);
                }
                return (int)_Value;
            }
      

  2.   

            public static double Round(double d, int i)
            {
                if (d >= 0)
                {
                    d += 5 * Math.Pow(10, -(i + 1));
                }
                else
                {
                    d += -5 * Math.Pow(10, -(i + 1));
                }
                if (i != 0)
                {                string str = d.ToString();
                    string[] strs = str.Split('.');
                    int idot = str.IndexOf('.');
                    string prestr = strs[0];
                    string poststr = strs[1];
                    if (poststr.Length > i)
                    {
                        poststr = str.Substring(idot + 1, i);
                    }
                    string strd = prestr + "." + poststr;
                    d = Double.Parse(strd);
                }
                return d;     
            }异常解决了,可以-0.44449保留2位小数结果是-0.44,还是不对啊
      

  3.   

    直接用这个不行吗decimal.Round()
      

  4.   

    这样怎么样?public static double MyRound(double d, int i)
    {
        return Math.Sign(d) * Math.Round(Math.Abs(d) + 1 * Math.Pow(10, -(i + 10)), i);
    }
      

  5.   

    另外你可以测试一下 Round(decimal d, int decimals, MidpointRounding mode) 重载是否可以正确得到你要得值。
      

  6.   

    呵呵,是 Round(double value, int digits, MidpointRounding mode) 重载!我测试了一下:?Math.Round(0.5,0,System.MidpointRounding.AwayFromZero)
    1.0
    ?Math.Round(-0.5,0,System.MidpointRounding.AwayFromZero)
    -1.0
      

  7.   

    2.0里面Math.Round提供了MidpointRounding枚举
    具体可以参考http://msdn.microsoft.com/zh-cn/library/system.midpointrounding.aspx
      

  8.   


    看下定义吧,对负数好像不行吧?using System.Runtime.InteropServices;namespace System
    {
        // 摘要:
        //     指定数学舍入方法应如何处理两个数字间的中间值。
        [ComVisible(true)]
        public enum MidpointRounding
        {
            // 摘要:
            //     当一个数字是其他两个数字的中间值时,会将其舍入为最接近的偶数。
            ToEven = 0,
            //
            // 摘要:
            //     当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。
            AwayFromZero = 1,
        }
    }
      

  9.   


    用这个测试如下代码,结果是-0.44,也不对        private void button3_Click(object sender, EventArgs e)
            {
                double d = bRound(-0.44449, 2);            MessageBox.Show(d.ToString());
            }
      

  10.   

    彻底迷糊了0.44449保留2位结果到底是0.44还是0.45呢?说啥的都有啊
    好像0.44才是正确的,excel也是显示0.44
    用D7测试了下,也是0.44,不是D7的rondto()用的啥标准
      

  11.   

    直接用这个...
    return Math.Round(value, decimals, MidpointRounding.AwayFromZero);刚经过测试,这个问题已经不存在了...我想应该是微软在.NET 2.0 SP1以后进行了修补...
      

  12.   

            // 摘要:
            //     当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。
            AwayFromZero = 1,
    ------------------
    不过这个描述上的错误仍然没有改...不知道微软中国的人都在干什么...你可以参考英文MSDN原文...AwayFromZero When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.你再去看看中文MSDN MidpointRounding 枚举中的描述...里面又清清楚楚地说...如果指定了 AwayFromZero,则将返回 -3,因为它是精度为零且与零最接近的数字。取绝对值较大的值
      

  13.   

    中文MSDN能不看最好不看,万不得已要看也要尽量少看。AwayFromZero When a number is halfway between two others, it is rounded toward the nearest number that is away from zero. 如果指定了 AwayFromZero,则将返回 -3,因为它是精度为零且与零最接近的数字。 这个明显是对AwayFromZero的翻译错误,AwayFromZero顾名思义就是离0远,实际含义就是当一个数字在其他2个中间则取整为最接近于离0远的那个。由于浮点数存在精度问题所以"the nearest"的修饰还是比较严谨的。
      

  14.   

    用这种方法试试
    float ave = float.Parse((x).ToString("F2"));
      

  15.   


    我用的是VS2010版本,经过“四舍五入”测试:
     Console.WriteLine(Math.Round(-0.5, 0, MidpointRounding.AwayFromZero)); 
    输出为:-1