close

I was having need that I need to pad number in 5 digit format. So following is a table in which format I need my leading zero format.

1-> 00001

20->00020

300->00300

4000->04000

50000->5000

 

So let’s create a simple console application and see how its works. Following is a code for that.

using System;
 
namespace LeadingZero
{
  class Program
  {
      static void Main(string[] args)
      {
          int a = 1;
          int b = 20;
          int c = 300;
          int d = 4000;
          int e = 50000;
 
          Console.WriteLine(string.Format("{0}------>{1}",a,a.ToString("D5")));
          Console.WriteLine(string.Format("{0}------>{1}", b, b.ToString("D5")));
          Console.WriteLine(string.Format("{0}------>{1}", c, c.ToString("D5")));
          Console.WriteLine(string.Format("{0}------>{1}", d, d.ToString("D5")));
          Console.WriteLine(string.Format("{0}------>{1}", e, e.ToString("D5")));
          Console.ReadKey();
      }
  }
}

As you can see in the above code I have use string.Format function to display value of integer and after using integer value’s ToString method. Now Let’s run the console application and following is the output as expected.

image

arrow
arrow
    文章標籤
    C#
    全站熱搜
    創作者介紹
    創作者 Kenneth 的頭像
    Kenneth

    Kenneth的部落格

    Kenneth 發表在 痞客邦 留言(0) 人氣()