我想让一个字符串变量的值始终是两位,不足两位时左边加0
即"7"-->"07"还有就是变量为int型时,如何变成上述格式,如7-->"07"貌似用Format吧,用c#不久,请指教。

解决方案 »

  1.   

    if(字符串.Length == 1)
    {
        字符串 = "0" + 字符串;
    }
      

  2.   


    string testA = "1";
                testA = testA.PadLeft(2, '0');
      

  3.   

    string testA = "1";
                testA = testA.PadLeft(2, '0');
      

  4.   

    if(字符串.Length == 1) 

        字符串 = "0" + 字符串; 
    }
    else if(字符串.Length >= 2)
    {
       字符串 = 字符串.SubString(0,2);
    }
      

  5.   

    楼上的 这个知道的
    我想知道用format怎么实现
    我有可能格式化成很长的字符串的,不可能先判断长度,再去补零
      

  6.   

    我好想记得int 可以直接.toString("D2")来转,
      

  7.   

    string testA = "1";
    testA = string.Format("{0}", testA.PadLeft(2, '0'));