C# Decimal ToString(String)

Description

Decimal ToString(String) converts the numeric value of this instance to its equivalent string representation, using the specified format.

Syntax

Decimal.ToString(String) has the following syntax.


public string ToString(
  string format
)

Parameters

Decimal.ToString(String) has the following parameters.

  • format - A standard or custom numeric format string (see Remarks).

Returns

Decimal.ToString(String) method returns The string representation of the value of this instance as specified by format.

Example

The following example displays a Decimal value using each of the supported standard numeric format specifiers, together with two custom numeric format strings.

In converting the numeric values to strings, the example uses the formatting conventions of the en-US culture.


using System;//from www . j av a 2 s.  com
public class MainClass{
  public static void Main(String[] argv){  
    decimal value = 12345.67m;
    string specifier;
    
    // Use standard numeric format specifiers.
    specifier = "G";
    System.Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));

    specifier = "C";
    System.Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));

    specifier = "E04";
    System.Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));

    specifier = "F";
    System.Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));

    specifier = "N";
    System.Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));

    specifier = "P";
    System.Console.WriteLine("{0}: {1}", specifier, (value/10000).ToString(specifier));
    
    // Use custom numeric format specifiers.
    specifier = "0,0.000";
    System.Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));

    specifier = "#,#.00#;(#,#.00#)";
    System.Console.WriteLine("{0}: {1}", specifier, (value*-1).ToString(specifier));
    
  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version