C# Double ToString(String)

Description

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

Syntax

Double.ToString(String) has the following syntax.


public string ToString(
  string format
)

Parameters

Double.ToString(String) has the following parameters.

  • format - A numeric format string.

Returns

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

Example

The following example displays several Double values using the supported standard numeric format specifiers together with three custom numeric format strings.


using System;/*from   w w w .j  a  v  a  2  s .c o m*/

public class MainClass{
  public static void Main(String[] argv){  
    double[] numbers= {1234.12345, -123456789.8377, 1.2345E21, 
                       -1.0573e-05};
    string[] specifiers = { "C", "E", "e", "F", "G", "N", "P", 
                            "R", "#,000.000", "0.###E-000",
                            "000,000,000,000.00###" };
    foreach (double number in numbers)
    {
       Console.WriteLine("Formatting of {0}:", number);
       foreach (string specifier in specifiers) {
          Console.WriteLine("   {0,-22} {1}", 
                            specifier + ":", number.ToString(specifier));
          // Add precision specifiers from 0 to 3. 
          if (specifier.Length == 1 & ! specifier.Equals("R")) {
             for (int precision = 0; precision <= 3; precision++) {
                string pSpecifier = String.Format("{0}{1}", specifier, precision);
                Console.WriteLine("   {0,-22} {1}", 
                                  pSpecifier + ":", number.ToString(pSpecifier));
             }    
             Console.WriteLine();
          }   
       }
       Console.WriteLine();
    }

  }
}

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