C# Padding an integer number with leading zeros

C# | String.Format() technique example: Here, we will figure out how to cushion a whole number with driving zero in C#?

To cushion a whole number with driving zero, we can utilize String.Format() strategy which is library technique for String class in C#.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Demo for pad zeros before an integer number:");

            Console.WriteLine(String.Format("{0:000000}", 256));
            Console.WriteLine(String.Format("{0:000000}", -256));          

            Console.WriteLine();  
        }
    }
}

Output:

Demo for pad zeros before an integer number:
000256
-000256

In the above program, we are printing 256 and – 256 numbers in 6 digits, as we realize that 256 has 3 digits and we are cushioning (left/driving) them with zeros.

Leave a Comment

error: Alert: Content is protected!!