Export (0) Print
Expand All

Double.ToString Method ()

.NET Framework 1.1

Converts the numeric value of this instance to its equivalent string representation.

[Visual Basic]
Overrides Overloads Public Function ToString() As String
[C#]
public override string ToString();
[C++]
public: String* ToString();
[JScript]
public override function ToString() : String;

Return Value

The string representation of the value of this instance.

Remarks

The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:

[sign]integral-digits[.[fractional-digits]][e[sign]exponential-digits]

Optional items are framed in square brackets ([ and ]). Items containing the term "digits" consist of a series of numeric characters ranging from 0 to 9.

sign
A negative sign or positive sign symbol.
integral-digits
A series of digits specifying the integral part of the number. Integral-digits can be absent if there are fractional-digits.
'.'
A culture-specific decimal point symbol.
fractional-digits
A series of digits specifying the fractional part of the number.
'e'
A lowercase character 'e', indicating exponential (scientific) notation.
exponential-digits
A series of digits specifying an exponent.

Some examples of the return value are "100", "-123,456,789", "123.45e+6", "500", "3.1416", "600", "-0.123", and "-Infinity".

This version of ToString uses the general format specifier ("G") and the NumberFormatInfo for the current culture.

Example

[Visual Basic, C#, C++] The following code sample illustrates the use of ToString:

[Visual Basic] 
Dim Done As Boolean = False
Dim Inp As String
Do

    Console.Write("Enter a real number: ")
    inp = Console.ReadLine()
    Try
        D = Double.Parse(inp)
        Console.WriteLine("You entered " + D.ToString() + ".")
        Done = True
    Catch E As FormatException
        Console.WriteLine("You did not enter a number.")
    Catch E As Exception
        Console.WriteLine("An exception occurred while parsing your response: " + E.ToString())
    End Try
Loop While Not Done

[C#] 
bool done = false;
string inp;
do 
{
    Console.Write("Enter a real number: ");
    inp = Console.ReadLine();
    try 
    {
        d = Double.Parse(inp);
        Console.WriteLine("You entered {0}.", d.ToString());
        done = true;
    } 
    catch (FormatException) 
    {
        Console.WriteLine("You did not enter a number.");
    }
    catch (Exception e) 
    {
        Console.WriteLine("An exception occurred while parsing your response: {0}", e.ToString());
    }
} while (!done);

[C++] 
bool done = false;
String* inp;
do {
   Console::Write(S"Enter a real number: ");
   inp = Console::ReadLine();
   try {
      d = Double::Parse(inp);
      Console::WriteLine(S"You entered {0}.", __box(d));
      done = true;
   } catch (FormatException*) {
      Console::WriteLine(S"You did not enter a number.");
   } catch (Exception* e) {
      Console::WriteLine(S"An exception occurred while parsing your response: {0}", e);
   }
} while (!done);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Double Structure | Double Members | System Namespace | Double.ToString Overload List | Formatting Overview | Parse | String

Show:
© 2014 Microsoft