The "0" custom format specifier in C#

CsharpProgrammingServer Side Programming

The “0” custom specifier is a zero placeholder.

If the value to be formatted has a digit in the position where the zero appears in the format string, the the digit is copied to the resultant string. However, if this doesn’t happen, then a zero appears.

Here is our double variable.

double d;
d = 292;

Now, by setting the following, you can easily make zero appear in the format string.

d.ToString("00000")

Example

 Live Demo

using System;
using System.Globalization;
class Demo {
   static void Main() {
      double d;
      d = 292;
      Console.WriteLine(d.ToString("00000"));
      Console.WriteLine(String.Format("{0:00000}", d));
      d = 3.5;
      Console.WriteLine(d.ToString("0.00", CultureInfo.InvariantCulture));
   }
}

Output

00292
00292
3.50
raja
Published on 14-Sep-2018 16:56:19
Advertisements