String Formatting with ToString in C#


To format a string, first set the value −

int value = 55;

Now to format the integer, use ToString and let’s say we need to set it for three places −

value.ToString("000");

The following is the complete code −

Example

 Live Demo

using System;
public class Program {
   public static void Main() {
      int value = 55;
      string res = value.ToString("000");
      Console.WriteLine(res);
   }
}

Output

055

Updated on: 22-Jun-2020

552 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements