Padding Zeroes to an integer value in C#

If you need to prefix zeroes to an integer when converting it to string values , you can do that by using the PadLeft method in c#.

Just specify the number of characters to prefix (padding) and the charater to prefix to the method and you are done ๐Ÿ™‚

Below is a sample sourcecode to demonstrate Padding Zeroes to an integer value in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    static class Program
    {
        static void Main()
        {
            FormatString();           
        }

        static void FormatString()
        {
           int intVal = 100;
	       string formattedString = intVal.ToString().PadLeft(4,'0');
	       MessageBox.Show(formattedString);
        }
    }  
}
Tags :

Leave Your Comment

Liked this content?. Please spread the word :)