1.第一个问题
string s1="4581241310264667"  //可能是任何位数的字符串   如s1这么一串数字不足19位的前面补0。
效果:0004581241310264667
2.第二个问题 
string s2="000000030400" //长度固定是12位,数值不固定。如s2在后2位前加上小数点,并将304.00填充在一个15位的空格内。
//且假定□代表空格
效果:□□□□□□□□□304.00

解决方案 »

  1.   

                string s1 = "4581241310264667";            while (s1.Length < 19)
                {
                    s1 = "0" + s1;
                }
      

  2.   

    [Quote=引用楼主 africa163 的回复:]
    1.第一个问题
    string s1="4581241310264667" //可能是任何位数的字符串  如s1这么一串数字不足19位的前面补0。
    效果:0004581241310264667
                string s1 = "4581241310264667";            while (s1.Length < 19)
                {
                    s1 = "0" + s1;
                }
      

  3.   

                string s2 = "000000030400";
                int p = s2.IndexOfAny("123456789".ToCharArray());
                s2 = "□□" + s2.Substring(0, p).Replace("0", "□") + s2.Substring(p, 10 - p) + "." + s2.Substring(10);
      

  4.   

    1: 
            private string foo(string s1)
            {
                if (s1 == null)
                    return new string('0', 19);
                else
                {
                    if (s1.Length > 19)
                        return s1;
                    else
                        return new string('0', 19 - s1.Length) + s1;
                }
            }2:        private string foo1(string s2)
            {
                if (s2 == null || s2.Length != 12)
                    throw new Exception("S2 必须是12位字符");
                 s2 = s2.Insert(10, ".");
                decimal d = 0;
                if (decimal.TryParse(s2, out d))
                {
                    s2 = d.ToString("0.00");
                    return new string(' ', 15 - s2.Length) + s2;
                }
                else
                {
                    throw new Exception("S2 字符串必须由数字组成");
                }
            }
      

  5.   

    string s1 = "4581241310264667" //可能是任何位数的字符串   
    s1 = s1.PadLeft(19, '0'); // 如s1这么一串数字不足19位的前面补0。
      

  6.   

                try
                {
                    string s1 = "4581241310264667";
                    string s2 = string.Format("{0:D19}", Convert.ToInt64(s1));
                    Console.WriteLine(s2);
                }
                catch (Exception )
                {
                    throw;
                }
      

  7.   

    string s2 = "000000030400"; //长度固定是12位,数值不固定。
    s2 = s2.Insert(s2.length - 2, ".").TrimStart('0').PadLeft(15);
    // 如s2在后2位前加上小数点,并将304.00 填充在一个15位的空格内。
      

  8.   

    方法很多,根据实际情况选择一个合适的就可以了
    string s1 = "4581241310264667";
    string result1 = s1.PadLeft(19, '0');string s2 = "000000030400";
    string result2 = (double.Parse(s2) / 100).ToString("N2").PadLeft(15, ' ');
      

  9.   

    简单易懂:        static void Main(string[] args)
            {
                string s1 = "4581241310264667";
                s1 = s1.PadLeft(19, '0');
                Console.WriteLine(s1);
                string s2 = "000000030400";
                int l = s2.Length;
                s2 = s2.Substring(0,l - 2) + '.' + s2.Substring(l - 2, 2);
                s2 = Regex.Replace(s2, "^0*", "").PadLeft(15, ' ');
                Console.WriteLine(s2);
                Console.ReadKey();
            }
    运行结果:
    0004581241310264667
             304.00
      

  10.   

    using System;class Program
    {
      static void Main()
      {
        string s1 = "4581241310264667"; // 可能是任何位数的字符串
        s1 = s1.PadLeft(19, '0');      // 如s1这么一串数字不足19位的前面补0。
        Console.WriteLine("[" + s1 + "]");    string s2 = "000000030400";    // 长度固定是12位,数值不固定。
        s2 = s2.Insert(s2.Length - 2, ".").TrimStart('0').PadLeft(15);
        // 如s2在后2位前加上小数点,并将304.00 填充在一个15位的空格内。
        Console.WriteLine("[" + s2 + "]");
      }
    }  
    /* 程序输出:
    [0004581241310264667]
    [         304.00]
    */
      

  11.   

    string s1 = "4581241310264667" //可能是任何位数的字符串   
    s1 = s1.PadLeft(19-S1.Length, '0'); // 如s1这么一串数字不足19位的前面补0。string s2="000000030400" //长度固定是12位,数值不固定。
    s2=s2.Substring(0,s2.Length-2)+"."+s2.Substring(s2.Length-2,2);
    s2=s2.PadLeft(12-s2.Length,'□');
      

  12.   

    string s="4581241310264667";
    s=s.PadLeft(19, '0');string s2="000000030400";
    s2=String.Formart("{0:N2}",s2.Substring(s3.Length-2)).PadLeft(15, ' ');
    ToString("#0.00");
      

  13.   

    string s2 = "000000030400"; // 长度固定是12位,数值不固定。 
    s2 = s2.Insert(s2.length - 2, ".").TrimStart('0').PadLeft(15).Replace(" .", "0.");
    // 如s2在后2位前加上小数点,并将304.00 填充在一个15位的空格内。