166.665 用math.round取2位是166.66,不是166.67..怎么办?`

解决方案 »

  1.   

    “Math.Round()准确的说,这个函数不是四舍五入,而是四舍六入五凑偶,就是说小于4或大于6的该舍该入是没有争议的,而5处在正中间,如果四舍五入则会造成数据的整体偏差,所以采取的原则是:如果舍入位为5,则舍入后最后一位为偶数,这是国际惯例。”
    上面的话是转来的
      

  2.   

    double d = 166.665;
    Response.Write(Math.Round(d, 2, MidpointRounding.AwayFromZero));
      

  3.   


    Console.WriteLine(Math.Round(166.665,2, MidpointRounding.AwayFromZero));
    //166.67
      

  4.   

    AwayFromZero枚举值表示从零开始,即0~4这五个会舍去,而5~9这五个会进位。
      

  5.   

    此方法的行为遵循 IEEE 标准 754 的第 4 节。这种舍入有时称为就近舍入或四舍六入五成双。它可以将因单方向持续舍入中点值而导致的舍入误差降到最低。Math.Round(3.44, 1); //Returns 3.4.
    Math.Round(3.45, 1); //Returns 3.4.
    Math.Round(3.46, 1); //Returns 3.5.
    若要控制 Math.Round(Decimal, Int32) 方法使用的舍入类型,请调用 Math.Round(Decimal, Int32, MidpointRounding) 重载。
      

  6.   

    ToEven  当一个数字是其他两个数字的中间值时,会将其舍入为最接近的偶数。
    AwayFromZero  当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。Console.WriteLine(Math.Round(166.665,2, MidpointRounding.AwayFromZero));的结果结果为什么不是166.66啊,不是取绝对值小的那个吗··
      

  7.   

    ToEven 当一个数字是其他两个数字的中间值时,会将其舍入为最接近的偶数。 
     AwayFromZero 当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。 
    可以试下:
    Math.Round(166.665, 2,MidpointRounding.ToEven/*若为正数,用绝对值较大的值*/); 
    试下了.
      

  8.   

    具体可以看下这里:
    http://msdn.microsoft.com/en-us/library/ms131274.aspx
      

  9.   

    翻译错误...原文...ToEven  When a number is halfway between two others, it is rounded toward the nearest even number. 
    AwayFromZero  When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.
      

  10.   

    int louzhu=166.665;
    int louzhuxiangyaodeshuzi=math.round(louzhu+0.001);
    OK 解决问题