首页 > 代码库 > 四舍五入的一些简单写法(利用内置函数,算法2种写法)

四舍五入的一些简单写法(利用内置函数,算法2种写法)

?
      //内置函数的写法
 
       //网上零售价和折扣价在计算结束需要进行进位,规则如下:
        //个位为1,2,3,4进位到5,例如计算后的价格为1201,则价格为1205;
        //个位为6,7,8,9进位到0,例如计算后的价格为1209.则价格为1210;
        public static string ChangePrice(double price)
        {
            int changed = 0;
            int m = (int)Math.Round(price);
            int n = (int)Math.Floor(price / 10);
            int d = m - n * 10;
            if (0 < d && d <= 4)
            {
                changed = n * 10 + 5;
            }
            else if (5 < d && d <= 9)
            {
                changed = n * 10 + 10;
            }
            else
            {
                changed = m;
            }
            return changed + "";
        }
 
//算法的写法
 public static string ChangePrice(double price)
        {
 
            int changed = 0;//改变后的值
            int m = 0;//四舍五入取整
            int n = 0;//取整数
 
            double temp = price;
            if (temp.ToString().IndexOf(‘.‘) > -1)
            {
                string strTemp = temp.ToString();
                strTemp = strTemp.Substring(0, strTemp.IndexOf(‘.‘));
                n = int.Parse(strTemp) / 10;//取整数
                m = (int)Math.Round(temp);//四舍五入取整数
                int d = m - n * 10;
                if (0 < d && d <= 4)
                {
                    changed = n * 10 + 5;
                }
                else if (5 < d && d <= 9)
                {
                    changed = n * 10 + 10;
                }
                else
                {
                    changed = m;
                }
 
            }
            else
            {
                string TempAgain = (temp / 10).ToString();
                if (TempAgain.IndexOf(‘.‘) > -1)
                {
                    TempAgain = TempAgain.Substring(0, TempAgain.IndexOf(‘.‘));
                    n = int.Parse(TempAgain);
                    m = int.Parse(temp.ToString());
 
                    int d = m - n * 10;
                    if (0 < d && d <= 4)
                    {
                        changed = n * 10 + 5;
                    }
                    else if (5 < d && d <= 9)
                    {
                        changed = n * 10 + 10;
                    }
                    else
                    {
                        changed = m;
                    }
                }
                else
                {
                    n = int.Parse(TempAgain);
                    m = int.Parse(temp.ToString());
 
                    int d = m - n * 10;
                    if (0 < d && d <= 4)
                    {
                        changed = n * 10 + 5;
                    }
                    else if (5 < d && d <= 9)
                    {
                        changed = n * 10 + 10;
                    }
                    else
                    {
                        changed = m;
                    }
                }
 
 
            }
            return changed + "";
 
 
        }