我现在一个做一个项目,用户提出了一个奇怪的要求,即四舍六入。
具体描述如下:一般情况下,如果要求保留两位小数,程序为自动采用四舍五入的方法。
现在问题是用户要求采用四舍六入方法进行进位。
有什么好的解决方法,各位达人请不吝指教,小弟不胜感激

解决方案 »

  1.   

    是不是按国际上的四舍五入的算法阿。
    你用Round看看是不是你要的
      

  2.   

    如果是5舍六入的话  可以
    double a=4.6;
    int b=(int)(a+0.4);
    b就是5舍六入后的值
      

  3.   

    double ccc = 5.1;
    int aaaa = (int)(ccc+0.5);
    int bbbb = (int)(ccc + 1.5);
    ls请看看bbbb是什么值,应该是7,确是6。
      

  4.   

    ls的爱钻牛角尖 
    我说了  不管什么值都加0.4再int 那么对于
    int bbbb = (int)(ccc + 1.5);
    应该是
    int bbbb = (int)(ccc + 1.5+0.4);
      

  5.   

    round 是国际标准进行的四舍五入
      

  6.   

    //对价格进行四舍五入
    string myPrice = String.Format("{0:N2}", Math.Round((Convert.ToDouble(price)*Convert.ToDouble(discount)*0.1),2));
      

  7.   

    不太明白楼主的意思,四舍六入,如果是6.5怎么办,你要的结果是6还是7?
    其实Math.Round就是五舍六入的
      

  8.   

    C#的Math.Rount实现的是四舍六入五成双的原则
    Console.WriteLine(Math.Round(11.4).ToString());=11
    Console.WriteLine(Math.Round(11.5).ToString());=12
    Console.WriteLine(Math.Round(11.6).ToString());=12
    Console.WriteLine(Math.Round(10.5).ToString());=10也就是说当是5的时候将取最接近的偶数。
      

  9.   

    来源:http://msdn2.microsoft.com/zh-cn/library/system.midpointrounding.aspx
    2.0才有四舍六入五成双这样的例子.1.0中我试了下没有.// This example demonstrates the Math.Round() method in conjunction 
    // with the MidpointRounding enumeration.
    using System;class Sample 
    {
        public static void Main() 
        {
        decimal result = 0.0m;
        decimal posValue =  3.45m;
        decimal negValue = -3.45m;// By default, round a positive and a negative value to the nearest even number. 
    // The precision of the result is 1 decimal place.    result = Math.Round(posValue, 1);
        Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue);
        result = Math.Round(negValue, 1);
        Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, negValue);
        Console.WriteLine();// Round a positive value to the nearest even number, then to the nearest number away from zero. 
    // The precision of the result is 1 decimal place.    result = Math.Round(posValue, 1, MidpointRounding.ToEven);
        Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, posValue);
        result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero);
        Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, posValue);
        Console.WriteLine();// Round a negative value to the nearest even number, then to the nearest number away from zero. 
    // The precision of the result is 1 decimal place.    result = Math.Round(negValue, 1, MidpointRounding.ToEven);
        Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, negValue);
        result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero);
        Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, negValue);
        Console.WriteLine();
        }
    }
    /*
    This code example produces the following results: 3.4 = Math.Round( 3.45, 1)
    -3.4 = Math.Round(-3.45, 1) 3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven)
     3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero)-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
    -3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)*/
      

  10.   

    Round--小数小于.5舍,大于.5入,等于.5则是向偶数方向入,这才是公平的!我还是在想楼主的四舍六入,5怎么办!
      

  11.   

    呵呵,我上网搜了一下,发现了一个
    四舍五入 与 四舍六入五成双 C#,vs.net.j#,vbscript都是采用的四舍六入五成双 
    SQL server 使用的是四舍五入double aa = 1.25;
    aa = Math.Round(aa,1);
    Response.Write(aa.ToString());
    返回的是1.2SQL 中 select round(1.25,1) 返回的是1.3
      

  12.   

    /// <summary>
    /// 转化
    /// </summary>
    /// <param name="OrignNum">要转换的数值</param>
    /// <param name="n">位数</param>
    /// <returns>转换后的字符串</returns>
    protected string ZH(double OrignNum,int n)
    {
    string[] str=OrignNum.ToString().Split(new char[]{'.'});
    if(str.Length<1 || str.Length>2)
    {
    return Math.PI.ToString();// 你可以替换为你认为不可能的数值
    }
    char[] splitNum=str[1].ToCharArray();
    if(n>=splitNum.Length)
    {
    return OrignNum.ToString();
    }
    int SplitW=int.Parse(splitNum[n].ToString());

    if(SplitW>=6)
    {
    int v=str[1].Substring(0,n).Length;

    str[1]=(int.Parse(str[1].Substring(0,n))+1).ToString();

    if(v<str[1].Length)
    {
    str[1]=str[1].Substring(1);
    str[0]=(int.Parse(str[0])+1).ToString();
    }
    }
    else
    {
    str[1]=str[1].Substring(0,n);
    }
    return str[0]+"."+str[1];
    }
    代码粗略验,如有bug请指正