Home > Backend Development > C#.Net Tutorial > How to convert integer to string with zero padding in C#?

How to convert integer to string with zero padding in C#?

WBOY
Release: 2023-08-23 23:01:02
forward
1589 people have browsed it

How to convert integer to string with zero padding in C#?

There are several ways to convert an integer to a string in C#.

PadLeft - Returns a new string of the specified length, where the beginning of the current string is padded with spaces or the specified Unicode characters

ToString - Returns a string representing the current object.

String Interpolation - The special character $ identifies a string literal as an interpolated string. This feature is available starting with C# 6.

Example of using string padding -

Example

Online demonstration

using System;
namespace DemoApplication{
   class Program{
      public static void Main(){
         int number = 5;
         Console.WriteLine("Number: {0}", number);
         var numberString = number.ToString().PadLeft(4, '0');
         Console.WriteLine("Padded String: {0}", numberString);
         Console.ReadLine();
      }
   }
}
Copy after login

Output

The above code The output is

Number: 5
Padded String: 0005
Copy after login
Copy after login
Copy after login
Copy after login

Example using explicit form

Example

Real-time demonstration

using System;
namespace DemoApplication{
   class Program{
      public static void Main(){
         int number = 5;
         Console.WriteLine("Number: {0}", number);
         var numberString = number.ToString("0000");
         Console.WriteLine("Padded String: {0}", numberString);
         Console.ReadLine();
      }
   }
}
Copy after login

Output

The output of the above code is

Number: 5
Padded String: 0005
Copy after login
Copy after login
Copy after login
Copy after login

Example using short format specifier

Example

Online demonstration

using System;
namespace DemoApplication{
   class Program{
      public static void Main(){
         int number = 5;
         Console.WriteLine("Number: {0}", number);
         var numberString = number.ToString("D4");
         Console.WriteLine("Padded String: {0}", numberString);
         Console.ReadLine();
      }
   }
}
Copy after login

Output

The output of the above code is

Number: 5
Padded String: 0005
Copy after login
Copy after login
Copy after login
Copy after login

Example using string interpolation

Example

Real-time demonstration

using System;
namespace DemoApplication{
   class Program{
      public static void Main(){
         int number = 5;
         Console.WriteLine("Number: {0}", number);
         var numberString = $"{number:0000}";
         Console.WriteLine("Padded String: {0}", numberString);
         Console.ReadLine();
      }
   }
}
Copy after login

Output

The output of the above code is

Number: 5
Padded String: 0005
Copy after login
Copy after login
Copy after login
Copy after login

The above is the detailed content of How to convert integer to string with zero padding in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template