double price = 2.07 现在要得到他末尾能被5除的最大和最小的数,比如定义 double Highprice 
double Lowprice 则Highprice= 2.10,Lowprice = 2.05如果
double price = 13.80则Highprice= 13.75,Lowprice = 13.85现求根据变量price来算出Highprice和Lowprice的最简单的方法

解决方案 »

  1.   

    int i
    for( i= 末尾数字-4; i<8;i++)
    {
    if imod5 =0; break
    }
    low= i high = i+0.05
      

  2.   

    现在要得到他末尾能被5除的最大和最小的数,比如定义 double Highprice 
    double Lowprice 则Highprice= 2.10,Lowprice = 2.05 
    难道2.95不是最大的啊,不知道是你抄错了还是我看不明白
    double price = 13.80 则Highprice= 13.75,//你上面都可以2.07变成2.10了,那这里你怎么最大的是13.75
    Lowprice = 13.85 //最小的是13.85
    乱七八糟的
      

  3.   

    LZ这个问题我想明白了
    我很快给你答案
    你先说一下
    你的price是几位长啊 
    固定码
      

  4.   

    double price = 2.07  
    double 型被5整除怎么说啊……
      

  5.   

    price是一个小数位后面两位的数字简单的说,就是找末尾离它最近的上下边界,该边界可以被5整除
      

  6.   

    如果确定是2位小数那就好办了啊int a=int.Parse(2.07*100)/5
    Highprice =(a+1)*5/100
    Lowprice =(a-1)*5/100
      

  7.   

     double price = 15.07;
            double Highprice = 0.0;
            double Lowprice = 0.0;        double tempdoub = 0.0;
            string tempstr = "";        tempstr = price.ToString("f1");//四舍五入
            if (price == Convert.ToDouble(tempstr))
            {
                Highprice = price + 0.05;
                Lowprice = price - 0.05;        }
            else
            {
                if (price < Convert.ToDouble(tempstr))
                {
                    Highprice = Convert.ToDouble(tempstr);
                    Lowprice = Convert.ToDouble(tempstr) - 0.5;
                }
                else
                {
                    Highprice = Convert.ToDouble(tempstr)+0.05;
                    Lowprice = Convert.ToDouble(tempstr); 
                }
            
            }
      

  8.   

    如果你的小数位是定死2位的 static void Main()
            {            double highprice = 0;
                double lowprice = 0;
                double price = 2.07;
                Count(price, ref highprice, ref lowprice);
                System.Console.WriteLine("price :{0}, highprice{1},lowprice{2} ", price, highprice, lowprice);
                Console.ReadKey();        }
            public static void Count(double price, ref  double highprice, ref double lowprice)
            {
                int tempprice = (int)(price * 100);
                int result = (tempprice / 5);
                highprice = ((double)result + 1) * 5 / 100;
                lowprice = ((double)result) * 5 / 100;
            }
      

  9.   

    哥们帮忙找BUG啊
    我想问LZ
    例如price=15.05
    用我的算法
    算出来
    Highprice = 15.1
    Lowprice = 15.05
    可不可以
      

  10.   

    没看明白,被5除是个什么概念?是整除,但除数又是小数(2.07)?看回复好象明白是在找最小的边界,比如说找207附近能够被5整除的最小边界,但是LZ又说是“到他末尾能被5除的最大和最小的数”,听糊涂了,看楼主级别那高,提出的问题怎么听不明白呢?难道CSDN的级别说明不了什么问题?
      

  11.   

     double price = 15.05;
            double Highprice = 0.0;
            double Lowprice = 0.0;        double tempdoub = 0.0;
            string tempstr = "";        tempstr = price.ToString("f1");//四舍五入
            if (price == Convert.ToDouble(tempstr))
            {
                Highprice = price + 0.05;
                Lowprice = price - 0.05;        }
            else
            {
                if (price < Convert.ToDouble(tempstr))
                {
                    Highprice = Convert.ToDouble(tempstr);
                    Lowprice = Convert.ToDouble(tempstr) - 0.05;
                    Lowprice = Convert.ToDouble(Lowprice.ToString("f2"));
                    if (Lowprice == price)
                    {
                        Lowprice = Lowprice - 0.05;
                    }
                }
                else
                {
                    Highprice = Convert.ToDouble(tempstr)+0.05;
                    Lowprice = Convert.ToDouble(tempstr); 
                }
            
            }    }
    问题解决
      

  12.   

    我求的代码是类似于:double Highprice = ((double)((int)(price * 100) / 5 + 1) * 5) / 100;
    double Lowprice = ((double)((int)(price * 100) / 5) * 5) / 100;类似这样几行代码就能得出答案的那种
      

  13.   

    原先想用Math.Round写一下的,不过想起今天早上看到有关于Math.Round进位问题的帖子,在完全弄明白前还是算了
      

  14.   


    double max = Math.Round(t, 1) > t? Math.Round(t, 1): Math.Round(t, 1) + 0.1;
    double min = max - 0.5;
      

  15.   

    弄错位了。。double max = Math.Round(t, 1) > t? Math.Round(t, 1): Math.Round(t, 1) + 0.1;
    double min = Math.Round(max - 0.05, 2);
      

  16.   

                int tempprice = (int)(price * 100);
                int result = (tempprice / 5);
                int result=(tempprice % 5); 余数
                 highprice = ((double)result + 1) * 5 / 100;
                 if(result>0)
                   lowprice = ((double)result) * 5 / 100;
                else
                    lowprice = ((double)result-1) * 5 / 100;
      

  17.   

    当t = 10.25时,楼上的min = 10.25 错!!!
      

  18.   

    试试            double Highprice = ((double)((int)(price * 100 ) / 5 + 1) * 5) / 100;
                double Lowprice = ((double)((int)(price * 100 - 1) / 5) * 5) / 100;
      

  19.   

    理解错了。。double max = Math.Round(t, 1) > t? Math.Round(t, 1): Math.Round(Math.Round(t, 1) + 0.05, 2);
    double min = Math.Round(max - t, 2) == 0.05? Math.Round(max - 0.10, 2): Math.Round(max - 0.05, 2);
      

  20.   

                double dNowPrice = 9.70;            double dHighPrice = ((double)((int)(dNowPrice * 100) / 5 + 1) * 5) / 100;
                double dLowPrice = ((double)((int)(dNowPrice * 100 - 1) / 5) * 5) / 100;dHighPrice = 9.70!!!错!!!应该为9.75!
      

  21.   

          double price = 13.80;           string pstr = price.ToString();
               int len= pstr.Length - pstr.IndexOf('.');
            
                double Highprice ;
                double Lowprice;
                          Highprice =((double)((int)(price * Math.Pow(10,len)) / 5 + 1) * 5) / Math.Pow(10,len);
              Lowprice=
              ((double)((int)(price * Math.Pow(10, len)) / 5) * 5) / Math.Pow(10, len);
      

  22.   


    Highprice = 13.85
    Lowprice  = 13.80!!!汗死,这个
      

  23.   

              int tempprice = (int)(price * 100);
                int result = (tempprice / 5);
                int result2=(tempprice % 5); //余数
                 highprice = ((double)result + 1) * 5 / 100;
                 if(result2>0)
                   lowprice = ((double)result) * 5 / 100;
                else
                    lowprice = ((double)result-1) * 5 / 100;
     我的代码肯定可以的.~
      

  24.   

    我明白了,我写帖子开始写错了double price = 13.80则Highprice= 13.85,Lowprice = 13.75在此更正
      

  25.   


                double price = 9.70;            int tempprice = (int)(price * 100);
                int result = (tempprice / 5);
                int result2 = (tempprice % 5); //余数
                double highprice;
                double lowprice;
                highprice = ((double)result + 1) * 5 / 100;
                if (result2 > 0)
                    lowprice = ((double)result) * 5 / 100;
                else
                    lowprice = ((double)result - 1) * 5 / 100;highprice = 9.70!!!错!!!
      

  26.   


                double Highprice = ((double)(((int)(price * 1000) + 1) / 50 + 1) * 50) / 1000;
                double Lowprice = ((double)((int)(price * 100 - 1) / 5) * 5) / 100;
    前提是LZ要能保证小数是只精确到第二位的
      

  27.   


    double price = 13.80的时候  Highprice= 13.85,Lowprice = 13.75 但是double price = 9.70的时候 Highprice= 9.7,Lowprice = 9.65很奇怪
      

  28.   


    不奇怪,你试下这个就明白了            Console.WriteLine(9.70 * 100);
                Console.WriteLine((int)(9.70 * 100));
      

  29.   


    int tempprice = int.Parse((price * 100).ToString());
                int result = (tempprice / 5);
                int result2 = (tempprice % 5);
                highprice = ((double)result + 1) * 5 / 100;
                if (result2 > 0)
                {
                    lowprice = ((double)result) * 5 / 100;
                }
                else
                {
                    lowprice = ((double)result - 1) * 5 / 100;
                }
     好吧 我被你打败了, 在(int)(price * 100); 转换的时候丢失了精度.
    换成int.Parse((price * 100).ToString());就行了. 或者你用其他方式确保不丢失精度也行~
      

  30.   


    非bug,lz的问题实际上跟数学关系不大,是计算机里浮点数表示的问题。
    计算机是二进制的,不能准确的表示十进制里的大部分浮点数,只是非常接近我们看到的那个值,有可能小于,也有可能大于。
    而在浮点转整型的过程中,C#是直接取整的,并不是四舍五入,所以会出现(int)(9.70*100)的结果是969的情况,因为9.70实际上是9.699999…,乘100后是969.9999…。
      

  31.   

    额,对于丢失精度这种情况,我觉得某种程度上也算是BUG吧