
- 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
C# Decimal ("D") Format Specifier
The "D" (or decimal) format specifier works for integer type. It converts a number to a string of decimal digits (0-9).
Let’say the following is our number.
int val = 467;
Now to return the result as 0467, use the following decimal format specifier.
val.ToString("D4")
Let us see another example.
Example
using System; using System.Globalization; class Demo { static void Main() { int val; val = 877; Console.WriteLine(val.ToString("D")); Console.WriteLine(val.ToString("D4")); Console.WriteLine(val.ToString("D8")); } }
Output
877 0877 00000877
- Related Articles
- Long Date ("D") Format Specifier in C#
- Short Date ("d") Format Specifier
- C# Currency ("C") Format Specifier
- Difference between %d and %i format specifier in C
- C# Exponential (“E”) Format Specifier
- C# Percent ("P") Format Specifier
- C# Numeric (“N”) Format Specifier
- C# Hexadecimal ("X") Format Specifier
- Difference between %d and %i format specifier in C language.
- C# Fixed-Point (“F”) Format Specifier
- Sortable ("s") Format Specifier in C#
- C# Round-trip ("R") Format Specifier
- Month ("M", "m") Format Specifier in C#
- Short Time ("t") Format Specifier in C#
- Long Time ("T") Format Specifier in C#

Advertisements