The "#" custom specifier in C#

The # custom format specifier in C# serves as a digit placeholder symbol in numeric formatting. It displays a digit only if one exists at that position, making it useful for creating flexible number formats that suppress unnecessary leading and trailing zeros.

Syntax

Following is the syntax for using the # format specifier −

number.ToString("#formatPattern")
String.Format("{0:#formatPattern}", number)

How It Works

When formatting a number, the # symbol represents an optional digit position. If the value has a digit at that position, it displays the digit. If not, nothing is displayed for that position. This behavior makes # different from 0 which always displays a digit (padding with zeros if necessary).

# vs 0 Format Specifiers # Specifier "#.##" ? 4.2 Optional digits Suppresses zeros 0 Specifier "0.00" ? 4.20 Required digits Pads with zeros

Using # for Decimal Formatting

Example

using System;
using System.Globalization;

class Demo {
    static void Main() {
        double d = 4.2;
        Console.WriteLine(d.ToString("#.##", CultureInfo.InvariantCulture));
        Console.WriteLine(String.Format(CultureInfo.InvariantCulture, "{0:#.##}", d));
        
        double precise = 123.456789;
        Console.WriteLine(precise.ToString("#.###"));
        Console.WriteLine(precise.ToString("#.#"));
    }
}

The output of the above code is −

4.2
4.2
123.457
123.5

Using # for Integer Formatting

Example

using System;

class Demo {
    static void Main() {
        double d = 345;
        Console.WriteLine(d.ToString("#####"));
        Console.WriteLine(String.Format("{0:#####}", d));
        
        int smallNumber = 42;
        Console.WriteLine(smallNumber.ToString("#####"));
        
        int largeNumber = 1234567;
        Console.WriteLine(largeNumber.ToString("###"));
    }
}

The output of the above code is −

345
345
42
1234567

Using # for Custom Patterns

Example

using System;

class Demo {
    static void Main() {
        double d = 74567989;
        Console.WriteLine(d.ToString("[##-##-##-##]"));
        Console.WriteLine(String.Format("{0:[##-##-##-##]}", d));
        
        int phoneNumber = 1234567890;
        Console.WriteLine(phoneNumber.ToString("(###) ###-####"));
        
        int accountNumber = 987654321;
        Console.WriteLine(accountNumber.ToString("###-##-####"));
    }
}

The output of the above code is −

[74-56-79-89]
[74-56-79-89]
(123) 456-7890
987-65-4321

Comparison

Format Pattern Value: 4.2 Value: 0.5 Description
#.## 4.2 .5 Optional digits, suppresses trailing zeros
0.00 4.20 0.50 Required digits, pads with zeros
#.00 4.20 .50 Mixed: optional integer, required decimal

Conclusion

The # custom format specifier provides flexible numeric formatting by displaying digits only when they exist at specific positions. It's particularly useful for creating clean number displays without unnecessary leading or trailing zeros, and for building custom formatted patterns like phone numbers or account codes.

Updated on: 2026-03-17T07:04:35+05:30

274 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements