例如,0.000000111111,我取0.00000011怎么取?

解决方案 »

  1.   

    如何取非零部分的两位,把非零部分转换为int型,然后和原来的string比较长度,假设长度少了A,那int转string截取2位,前面加A个0就行了
      

  2.   


     string str = "0.000000111111";
     str = str.Substring(0, str.LastIndexOf("0") + 3);
     Console.WriteLine(str);                
      

  3.   


                decimal d = 0.000000111111m;
                string temp = Regex.Replace(d.ToString(), @"(?<=[^0.]{2}).+", "");            decimal res = Convert.ToDecimal(temp);            Console.WriteLine(res.ToString());
      

  4.   

    数值运算,性能更好。试试这个:帮你四舍五入。
     double x = 0.000111111;
                double tx = x;
                int n = 0;
                while (tx<1)
                {
                    tx *= 10;
                    n++;
                }
                Console.WriteLine(Math.Round(x, n + 1));
      

  5.   

    double a = 0.000000111111;
    string b = a.ToString("0.00000000");
      

  6.   

    string str = num.ToString();
     str = str.Substring(0, str.LastIndexOf("0") + 3);
    double dLast = str.ToDouble();
      

  7.   

    上面我发的错了,要是0.0000011011的情况就有问题
    9楼的代码不错,稍微改一下
    double x = 0.000111111;万一是2.00000011呢
                double tx = x;
                int n = 0;
                while (tx * 10 % 10 == 0)//取出小数位
                {
                    tx *= 10;
                    n++;
                }
                Console.WriteLine(Math.Round(x, n + 1));
      

  8.   

    decimal dc=2.1546541641646
    dc.stostring("f2")
    好象是这样了.
      

  9.   

    double d = 0.000000234210000001111;
    double tmp = d - (long)d;
    int digit = 0;
    while (tmp < 10)
    {
         tmp *= 10;
         digit++;
    }
    double result = Math.Round(d, digit);
      

  10.   


    正则的反向预搜索是个好法子,只可惜如果是double类型,ToString()的结果不好预测.
      

  11.   


    double a=0.000000111111;
    string values=string.Format("{0:F8}",a);
      

  12.   

    0.000000111111这个数字在A1单元格则公式=LEFT(A1,10)得出的数字是10位