Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

I am Jalpesh Vadgama a Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 6 years of experience in .NET Technology.
Browse by Tags · View All
#ASP.NET 88
#DOTNET 87
BRH 79
ASP.NET 72
#.NET 52
C#.NET 48
DOTNET 47
ASP.NET 4.0 31
ASP.NET MVC 29
VisualStudio 27

Archive · View All
December 2010 16
July 2011 13
April 2011 13
April 2012 12
January 2011 12
June 2011 11
May 2011 11
May 2012 8
February 2013 7
January 2013 7

How to pad number with leading zero with C#

Apr 4 2012 3:33AM by Jalpesh   

Recently I was working with a project where I was in need to format a number in such a way which can apply leading zero for particular format.  So after doing such R and D I have found a great way to apply this leading zero format.

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 in the above example you can see that 1 will become 00001 and 20 will become 00200 format so on. So to display an integer value in decimal format I have applied interger.Tostring(String) method where I have passed “Dn” as the value of the format parameter, where n represents the minimum length of the string. So if we pass 5 it will have padding up to 5 digits.

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

Here you can see the integer number are converted into the exact output that we requires. That’s it you can see it’s very easy. We have written code in nice clean way and without writing any extra code or loop. Hope you liked it. Stay tuned for more.. Till than happy programming.

Shout it

Tags: C#.NET, #DOTNET, #.NET, VB.NET,


Jalpesh
15 · 11% · 3548
2
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"How to pad number with leading zero with C#" rated 5 out of 5 by 2 readers
How to pad number with leading zero with C# , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]