TechieClues TechieClues
Updated date Mar 17, 2023
In this blog, we will learn how to convert an Int (Integer) to a string with padding zeros in C#

We will learn how to convert an Int (Integer) to a string with padding zeros using C#. We have multiple ways to convert an int to a string in C# as mentioned below,

  • Using String.Format() method
  • Using Int32.ToString() method
  • Using String.PadLeft() method
  • Using String Interpolation

Using String.Format() method

String.Format() method can be used to format the specified object. The format needs to be specified in the String.Format() method. The below example String.Format() method formats an Int value using the “D” format specifier with a precision specifier of 6,

using System;

namespace ConvertIntToStringWithPad0
{
    class Program
    {
        public static void Main()
        {
            int num = 100;
            
            string str = String.Format("{0:D6}", num);

            Console.WriteLine(str);
            Console.ReadKey();
        }
    }
}

Output:
 

000100

Using Int32.ToString() method

Int32.ToString() method can be used to convert a number or numeric value to its equivalent string representation. we can pass the custom numeric format to Int32.ToString() overload method. Below example, we use the "D” format specifier with a precision specifier of 6 to format the Int value.

using System;

namespace ConvertIntToStringWithPad0
{
    class Program
    {
        public static void Main()
        {
            int num = 100;

            var str = num.ToString("D7");

            Console.WriteLine(str);
            Console.ReadKey();
        }
    }
}

Output:
 

0000100

Using String.PadLeft() method

String.PadLeft() method is used to return a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode character. Below example, we provide the totalwidth to 10 so the method constructs the 10-digit string from the given number value.

using System;

namespace ConvertIntToStringWithPad0
{
    class Program
    {
        public static void Main()
        {
            int num = 100;

            string str = num.ToString().PadLeft(10, '0');

            Console.WriteLine(str);
            Console.ReadKey();
        }
    }
}

 Output:
 

0000000100

Using String Interpolation

Alternatively, we can use the C# string interpolation $ to format the specified string. The below example demonstrates how to convert the number to the specified format.

using System;

namespace ConvertIntToStringWithPad0
{
    class Program
    {
        public static void Main()
        {
            int num = 100;

            string str = $"{num:D6}";

            Console.WriteLine(str);
            Console.ReadKey();
        }
    }
}

Output:

0000000100

 

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!