C# Decimal ("D") Format Specifier

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;//from  w  ww . j av a  2  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.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var