码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-05-31 21:56:50      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   a   int   

      //内置函数的写法
 
       //网上零售价和折扣价在计算结束需要进行进位,规则如下:
        //个位为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 + "";
 
 
        }

 

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

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

标签:c   class   blog   code   a   int   

原文地址:http://www.cnblogs.com/it888/p/3760963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!