C# Decimal ("D") Format

In this chapter you will learn:

  1. Use Decimal ("D") Format
  2. Example to Use Decimal ("D") Format

Description

The "D" (or decimal) format specifier converts a number to a string of decimal digits (0-9), prefixed by a minus sign if the number is negative.

Item Value
Format specifier "D" or "d"
Supported by Integral types only.
Result Integer digits with optional negative sign.
Precision specifier The precision specifier indicates the minimum number of digits desired in the resulting string.
Default precision specifier Minimum number of digits required.

Example,

Value Format Formatted
1234("D") 1234
-1234 ("D6") -001234

If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.

NegativeSign from NumberFormatInfo defines the string that indicates that a number is negative.

Example


using System;//w  ww  .  j  ava2 s. c o  m

class MainClass
{
   static void Main()
   {
        int value; 
        
        value = 12345;
        Console.WriteLine(value.ToString("D"));
        // Displays 12345
        Console.WriteLine(value.ToString("D8"));
        // Displays 00012345 
        
        value = -12345;
        Console.WriteLine(value.ToString("D"));
        // Displays -12345
        Console.WriteLine(value.ToString("D8"));
        // Displays -00012345
   }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Use Exponential ("E") Format
  2. Example to use Exponential ("E") Format Specifier
Home »
  C# Tutorial »
    C# Language »
      C# Standard Data Type Format
C# Standard Numeric Format
C# Currency ("C") Format
C# Decimal ("D") Format
C# Exponential ("E") Format
C# Fixed-Point ("F") Format
C# General ("G") Format
C# Hexadecimal ("X") Format
C# Numeric ("N") Format
C# Percent ("P") Format
C# Round-trip ("R") Format