The "#" custom specifier in C#

CsharpProgrammingServer Side Programming

The "#" custom format specifier works as a digit-placeholder symbol.

If the value to be formatted has a digit in the position where the "#" symbol appears in the format string, then that digit is copied to the resultant string.

We have set a double type here.

double d;
d = 4.2;

Now, let us use the “#” custom specifier.

d.ToString("#.##", CultureInfo.InvariantCulture)

Here are other examples.

Example

 Live Demo

using System;
using System.Globalization;
class Demo {
   static void Main() {
      double d;
      d = 4.2;
      Console.WriteLine(d.ToString("#.##", CultureInfo.InvariantCulture));
      Console.WriteLine(String.Format(CultureInfo.InvariantCulture,"{0:#.##}", d));
      d = 345;
      Console.WriteLine(d.ToString("#####"));
      Console.WriteLine(String.Format("{0:#####}", d));
      d = 74567989;
      Console.WriteLine(d.ToString("[##-##-##-##]"));
      Console.WriteLine(String.Format("{0:[##-##-##-##]}", d));
   }
}

Output

4.2
4.2
345
345
[74-56-79-89]
[74-56-79-89]
raja
Published on 14-Sep-2018 13:27:50
Advertisements