private void Form1_Load(object sender, EventArgs e)
        {
            string fmt = "0000000000";
            double intValue = 2010;

            string s = intValue.ToString(fmt);
            //출력은 0000002010
            this.label1.Text =s;
     
            
        }

끝~

 

아래는 참고하세요

특정 길이까지 숫자 값 앞에 0을 채우려면

string fmt = "00000000.##";   
int intValue = 1053240;   
decimal decValue = 103932.52m;   
float sngValue = 1549230.10873992f;   
double dblValue = 9034521202.93217412;   

outputBlock.FontFamily = new System.Windows.Media.FontFamily("Courier New");   

// Display the numbers using the ToString method. 
outputBlock.Text += intValue.ToString(fmt) + "\n";   
outputBlock.Text += decValue.ToString(fmt) + "\n";   
outputBlock.Text += sngValue.ToString(fmt) + "\n";   
outputBlock.Text += sngValue.ToString(fmt) + "\n";   
outputBlock.Text += "\n";   

// Display the numbers using composite formatting. 
string formatString = " {0,15:" + fmt + "}\n";   
outputBlock.Text += String.Format(formatString, intValue);   
outputBlock.Text += String.Format(formatString, decValue);   
outputBlock.Text += String.Format(formatString, sngValue);   
outputBlock.Text += String.Format(formatString, dblValue);   
// The example displays the following output: 
//       01053240 
//       00103932.52 
//       01549230 
//       01549230 
//        
//               01053240 
//            00103932.52 
//               01549230 
//          9034521202.93    

 

특정 길이까지 정수 앞에 0을 채우려면?

byte byteValue = 254;
short shortValue = 10342;
int intValue = 1023983;
long lngValue = 6985321;
ulong ulngValue = UInt64.MaxValue;

outputBlock.FontFamily = new System.Windows.Media.FontFamily("Courier New");

// Display integer values by caling the ToString method.
outputBlock.Text += String.Format("{0,22} {1,22}", byteValue.ToString("D8"), byteValue.ToString("X8")) + "\n";
outputBlock.Text += String.Format("{0,22} {1,22}", shortValue.ToString("D8"), shortValue.ToString("X8")) + "\n";
outputBlock.Text += String.Format("{0,22} {1,22}", intValue.ToString("D8"), intValue.ToString("X8")) + "\n";
outputBlock.Text += String.Format("{0,22} {1,22}", lngValue.ToString("D8"), lngValue.ToString("X8")) + "\n";
outputBlock.Text += String.Format("{0,22} {1,22}", ulngValue.ToString("D8"), ulngValue.ToString("X8")) + "\n";
outputBlock.Text += "\n";

// Display the same integer values by using composite formatting.
outputBlock.Text += String.Format("{0,22:D8} {0,22:X8}", byteValue) + "\n";
outputBlock.Text += String.Format("{0,22:D8} {0,22:X8}", shortValue) + "\n";
outputBlock.Text += String.Format("{0,22:D8} {0,22:X8}", intValue) + "\n";
outputBlock.Text += String.Format("{0,22:D8} {0,22:X8}", lngValue) + "\n";
outputBlock.Text += String.Format("{0,22:D8} {0,22:X8}", ulngValue) + "\n";
// The example displays the following output:
//                     00000254               000000FE
//                     00010342               00002866
//                     01023983               000F9FEF
//                     06985321               006A9669
//         18446744073709551615       FFFFFFFFFFFFFFFF
//       
//                     00000254               000000FE
//                     00010342               00002866
//                     01023983               000F9FEF
//                     06985321               006A9669
//         18446744073709551615       FFFFFFFFFFFFFFFF
//         18446744073709551615       FFFFFFFFFFFFFFFF

 

정수 앞에 특정 개수의 0을 채우려면?

  • 정수 값에서 표시할 선행 0의 개수를 결정합니다.

  • 정수를 10진수 값으로 표시할지 또는 16진수 값으로 표시할지를 결정합니다.10진수 값으로 형식을 지정하려면 "D" 표준 형식 지정자를 사용해야 합니다. 16진수 값으로 형식을 지정하려면 "X" 표준 형식 지정자를 사용해야 합니다.

  • 정수 값의 ToString("D").Length 또는 ToString("X").Length 메서드를 호출하여 0이 채워지지 않은 숫자 문자열의 길이를 확인합니다.

  • 형식이 지정된 문자열에 포함하려는 선행 0의 개수를 0이 채워지지 않은 숫자 문자열의 길이에 더합니다.이 값은 0이 채워진 문자열의 총 길이를 정의합니다.

  • 정수 값의 ToString(String) 메서드를 호출하고 문자열 "Dn"(10진수 문자열의 경우) 및 "Xn"(16진수 문자열의 경우)을 전달합니다. 여기서 n은 0이 채워진 문자열의 총 길이를 나타냅니다.복합 형식 지정을 지원하는 메서드에 "Dn" 또는 "Xn" 형식 문자열을 사용할 수도 있습니다.

  • int value = 160934;
    int decimalLength = value.ToString("D").Length + 5;
    int hexLength = value.ToString("X").Length + 5; 
    
    outputBlock.FontFamily = new System.Windows.Media.FontFamily("Courier New"); 
    
    outputBlock.Text += value.ToString("D" + decimalLength.ToString()) + "\n";
    outputBlock.Text += value.ToString("X" + hexLength.ToString()) + "\n";
    // The example displays the following output:
    //       00000160934
    //       00000274A6      
    

    + Recent posts