
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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
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
- Related Articles
- The "%" custom specifier in C#
- The "#" custom specifier in C#
- "." custom specifier in C#
- C# Currency ("C") Format Specifier
- Sortable ("s") Format Specifier in C#
- C# Exponential (“E”) Format Specifier
- C# Percent ("P") Format Specifier
- C# Numeric (“N”) Format Specifier
- C# Decimal ("D") Format Specifier
- C# Hexadecimal ("X") Format Specifier
- Month ("M", "m") Format Specifier in C#
- Short Time ("t") Format Specifier in C#
- Long Time ("T") Format Specifier in C#
- Year Month ("Y") Format Specifier in C#
- Long Date ("D") Format Specifier in C#

Advertisements