例如3.3在转成整数时为3,-3.8在转成整数时为-4。

解决方案 »

  1.   

    试加正负0.5一下
                Console.WriteLine((int)(3.3 + 0.5));
                Console.WriteLine((int)(-3.8 - 0.5));
      

  2.   

    Math.Floor(3.8)//返回小于或等于指定数字的最大整数,结果为3
    Math.Round(3.3)//将值舍入到最接近的整数或指定的小数位数,结果为4
      

  3.   

    不好意思,写错了,更改如下:
    Math.Floor(3.8)//返回小于或等于指定数字的最大整数,结果为3
    Math.Round(3.8)//将值舍入到最接近的整数或指定的小数位数,结果为4
      

  4.   


    Round肯定是不对的
                double x = -3.8;
                int y = 0;
                if (x != 0)
                    y = x > 0 ? (int)(x + 0.5) : (int)(x - 0.5);
                Console.WriteLine(y);
      

  5.   

    Math.Round
    系统已经考虑正负数了。
      

  6.   

    请问Convert.ToInt32(-3.8)等于几?
    Convert.ToInt32(3.3)等于几?
      

  7.   

    Math.Round实现的是banker舍入法http://hi.baidu.com/frwl/blog/item/eb64c39537ab7f4fd0135e20.html
      

  8.   

    不能直接调用Math.Round方法的,这可和Java的不一样哦Math.Round这个函数的解释是将值按指定的小数位数舍入,并不就是四舍五入。这种舍入有时称为就近舍入或四舍六入五成双Math.Round(0.4)  //result:0 
    Math.Round(0.6)  //result:1 
    Math.Round(0.5)  //result:0 
    Math.Round(1.5)  //result:2 
    Math.Round(2.5)  //result:2 
    Math.Round(3.5)  //result:4 
    Math.Round(5.5)  //result:6 
    Math.Round(6.5)  //result:6 
    Math.Round(8.5)  //result:8 
    Math.Round(9.5)  //result:10可以看出 并不是四舍五入的  
    其实在 VB, VBScript, C#, J#, T-SQL 中 Round 函数都是采用 Banker's rounding(银行家舍入)算法,即四舍六入五取偶。事实上这也是 IEEE 规定的舍入标准。因此所有符合 IEEE 标准的语言都应该是采用这一算法的。请调用  Math.Round(Decimal, MidpointRounding) 重载!~哦,原来还有重载的方法可用,MidpointRounding在两个数字之间时如何舍入的规范,规范MidpointRounding中它有2个成员,一个是ToEven还有个是AwayFromZero。//四舍五入  
    Math.Round(0.5,MidpointRounding.AwayFromZero)
      

  9.   

    以前我也碰到这个问题  虽然知道了Math.Round(0.5,MidpointRounding.AwayFromZero)来解决,但是MidpointRounding.AwayFromZero里面是如何算的 我还没搞清楚  我还写了一片文章总结下 但MidpointRounding.AwayFromZero的MSDN中的解释令我很难理解(没空去研究了),结果写了一半就搁置在哪了  兄弟是否能去研究下看看
    http://blog.csdn.net/chopper7278/archive/2008/12/02/3429388.aspx
      

  10.   

    岂敢, wuyazhe兄 能不能帮我去看看msdn对MidpointRounding.AwayFromZero的解释  我实在不解阿... 现在主要都没时间去看了  拿出来大家讨论下看看msdn对AwayFromZero的解释: 当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。  
    这个...想不出来了哦...
      

  11.   


    double a = 2.3;
    double b = 4.5;
    Response.Write("a:"+a.ToString("0")+"<br>");
    Response.Write("b:"+b.ToString("0"));------------
    a:2
    b:5 
      

  12.   

    这叫银行家舍入法...可看一下我在近3年前写的老文章...http://blog.csdn.net/vrhero/archive/2007/07/29/1714889.aspx
      

  13.   

    这句话你想不出来很正常,因为这是MSDN中文译版的一个错误,始终没改正...
      

  14.   

    对了,还要补充一下...这个MidpointRounding.AwayFromZero枚举现在已经变成四舍五入算法了,那个解释已经不通了...
      

  15.   

    Convert.ToInt32(-3.8)这是很万能的转换!
      

  16.   


    都说成这样了。。还没找到答案?//四舍五入  
    Math.Round(0.5,MidpointRounding.AwayFromZero)这样就可以了
      

  17.   


    Convert.ToInt32 采用的是四舍六入五成双的方法处理小数的,所以某些时候会有问题的。
    Convert.ToInt32(3.5); //4
    Convert.ToInt32(-3.5); //-4
    Convert.ToInt32(4.5); //4
    Convert.ToInt32(-4.5); //-4
      

  18.   

    Math.Round(0.5,MidpointRounding.AwayFromZero)  貌似正解...