The "0" custom format specifier in C#


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements