C#的话用“G"的精度表示保留有效数字位数,例如保留4位var arr = new double[] { 123.4567, 12345670, 12.34567, 0.001234567 };
foreach (var d in arr)
  Console.WriteLine(d.ToString("G4"));//数据
123.5
1.235E+07
12.35
0.001235

解决方案 »

  1.   

    正则- -。。爱几位就几位
    \d+[\.]\d{几位}
    -----------------
     static void Main(string[] args)
            {
                Program p = new Program();
                p.aa();
            }        public void aa()
            {
                string ss = "123.456";
                ss = Regex.Match(ss.ToString(), @"\d+[\.]\d{3}", RegexOptions.IgnoreCase).ToString();
                Console.WriteLine(ss);
                Console.Read();
            }
      

  2.   

    哦,没看清楚。4L的代码可以参考,不过是基于字符串的。其实有效数字的算法很简单。using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                double[] data = new double[] 
                {
                    123.456,
                    10.334,
                    108.251231,
                    2000.0,
                    2000123.0,
                    0.0000123456,
                    0.000031415926,
                    0.0,
                    -0.0,
                    -0.0007,
                    -0.0523,
                    -0.005432354,
                    -0.9999,
                    -1.0,
                    -12.2345,
                    -100.0,
                    -12345.2345,
                    -15555.55555
                };
                for (int i = 0; i <= 5; i++)
                {
                    string result = string.Join("\r\n", data.Select(x => string.Format("\t{0} -> {1}", x, foo(x, i))));
                    Console.WriteLine(i.ToString() + ":\r\n" + result + "\r\n");
                }
            }        static double foo(double d, int n)
            {
                if (d == 0.0) return 0;
                if (d > 1 || d < -1)
                    n = n - (int)Math.Log10(Math.Abs(d)) - 1;
                else
                    n = n + (int)Math.Log10(1.0 / Math.Abs(d));
                if (n < 0)
                {
                    d = (int)(d / Math.Pow(10, 0 - n)) * Math.Pow(10, 0 - n);
                    n = 0;
                }
                return Math.Round(d, n);
            }
        }
    }
    0:
            123.456 -> 0
            10.334 -> 0
            108.251231 -> 0
            2000 -> 0
            2000123 -> 0
            1.23456E-05 -> 0
            3.1415926E-05 -> 0
            0 -> 0
            0 -> 0
            -0.0007 -> -0.001
            -0.0523 -> -0.1
            -0.005432354 -> -0.01
            -0.9999 -> -1
            -1 -> -1
            -12.2345 -> 0
            -100 -> 0
            -12345.2345 -> 0
            -15555.55555 -> 01:
            123.456 -> 100
            10.334 -> 10
            108.251231 -> 100
            2000 -> 2000
            2000123 -> 2000000
            1.23456E-05 -> 1E-05
            3.1415926E-05 -> 3E-05
            0 -> 0
            0 -> 0
            -0.0007 -> -0.0007
            -0.0523 -> -0.05
            -0.005432354 -> -0.005
            -0.9999 -> -1
            -1 -> -1
            -12.2345 -> -10
            -100 -> -100
            -12345.2345 -> -10000
            -15555.55555 -> -100002:
            123.456 -> 120
            10.334 -> 10
            108.251231 -> 100
            2000 -> 2000
            2000123 -> 2000000
            1.23456E-05 -> 1.2E-05
            3.1415926E-05 -> 3.1E-05
            0 -> 0
            0 -> 0
            -0.0007 -> -0.0007
            -0.0523 -> -0.052
            -0.005432354 -> -0.0054
            -0.9999 -> -1
            -1 -> -1
            -12.2345 -> -12
            -100 -> -100
            -12345.2345 -> -12000
            -15555.55555 -> -150003:
            123.456 -> 123
            10.334 -> 10.3
            108.251231 -> 108
            2000 -> 2000
            2000123 -> 2000000
            1.23456E-05 -> 1.23E-05
            3.1415926E-05 -> 3.14E-05
            0 -> 0
            0 -> 0
            -0.0007 -> -0.0007
            -0.0523 -> -0.0523
            -0.005432354 -> -0.00543
            -0.9999 -> -1
            -1 -> -1
            -12.2345 -> -12.2
            -100 -> -100
            -12345.2345 -> -12300
            -15555.55555 -> -155004:
            123.456 -> 123.5
            10.334 -> 10.33
            108.251231 -> 108.3
            2000 -> 2000
            2000123 -> 2000000
            1.23456E-05 -> 1.235E-05
            3.1415926E-05 -> 3.142E-05
            0 -> 0
            0 -> 0
            -0.0007 -> -0.0007
            -0.0523 -> -0.0523
            -0.005432354 -> -0.005432
            -0.9999 -> -0.9999
            -1 -> -1
            -12.2345 -> -12.23
            -100 -> -100
            -12345.2345 -> -12340
            -15555.55555 -> -155505:
            123.456 -> 123.46
            10.334 -> 10.334
            108.251231 -> 108.25
            2000 -> 2000
            2000123 -> 2000100
            1.23456E-05 -> 1.2346E-05
            3.1415926E-05 -> 3.1416E-05
            0 -> 0
            0 -> 0
            -0.0007 -> -0.0007
            -0.0523 -> -0.0523
            -0.005432354 -> -0.0054324
            -0.9999 -> -0.9999
            -1 -> -1
            -12.2345 -> -12.234
            -100 -> -100
            -12345.2345 -> -12345
            -15555.55555 -> -15556Press any key to continue . . .
      

  3.   

    Math.Round函数,或者用字符串格式输出。
      

  4.   

    谢谢。还有一个特殊的要求就是比如1.5,保留3位有效数字变成1.50,我写了下代码,供以后有人需要时用:#region 保留有效数字
            public string EffectiveNumber(string value, string precision)
            {
                Decimal newValue = Convert.ToDecimal(value);
                string newStr = newValue.ToString("G" + precision);
                string str = Convert.ToDecimal(Double.Parse(newStr)).ToString();
                int i = 0;
                bool isCheck = false;
                if (str.IndexOf(".") > -1)
                {
                    string[] values = str.Split('.');
                    foreach (string s in values)
                    {
                        foreach (char c in s)
                        {
                            if (c != '0')
                            {
                                isCheck = true;
                                i++;
                            }
                            else
                            {
                                if (isCheck == true)
                                {
                                    i++;
                                }
                            }
                        }
                    }
                }
                if (i != 0 && i < Convert.ToDouble(precision))
                {
                    for (int j = i; j < Convert.ToInt32(precision); j++)
                    {
                        str += "0";
                    }
                }            return str;
            }
            #endregion