Aracılığıyla paylaş


Double.ToString Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Overrides Function ToString As String
[SecuritySafeCriticalAttribute]
public override string ToString()

Return Value

Type: System.String
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 elements are framed in square brackets ([ and ]). Elements containing the term "digits" consist of a series of numeric characters ranging from 0 to 9. The elements listed in the following table are supported:

Element

Description

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 the ToString method implicitly uses the general numeric format specifier ("G") and the NumberFormatInfo for the current culture.

The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 ToString does not return the correct string containing comma delimiters between group digits when a custom format is used.

Examples

The following example uses the default Double.ToString() method to display the string representations of a number of Double values.

Dim number As Double

number = 1.6E+20
' Displays 1.6E+20.      
outputBlock.Text &= number.ToString() & vbCrLf

number = 160.0
' Displays 160.
outputBlock.Text &= number.ToString() & vbCrLf

number = -3.541
' Displays -3.541.
outputBlock.Text &= number.ToString() & vbCrLf

number = -150234.5222199
' Displays -150234.5222199.
outputBlock.Text &= number.ToString() & vbCrLf

number = -15023452221.9902
' Displays -15023452221.9902.
outputBlock.Text &= number.ToString() & vbCrLf

number = 0.60344
' Displays 0.60344.
outputBlock.Text &= number.ToString() & vbCrLf

number = 0.000000001
' Displays 1E-09.
outputBlock.Text &= number.ToString() & vbCrLf
double number;

number = 1.6E20;
// Displays 1.6E+20.
outputBlock.Text += number.ToString() + "\n";

number = 1.6E2;
// Displays 160.
outputBlock.Text += number.ToString() + "\n";

number = -3.541;
// Displays -3.541.
outputBlock.Text += number.ToString() + "\n";

number = -1502345222199E-07;
// Displays -150234.5222199.
outputBlock.Text += number.ToString() + "\n";

number = -15023452221990199574E-09;
// Displays -15023452221.9902.
outputBlock.Text += number.ToString() + "\n";

number = .60344;
// Displays 0.60344.
outputBlock.Text += number.ToString() + "\n";

number = .000000001;
// Displays 1E-09.
outputBlock.Text += number.ToString() + "\n";

The following example illustrates the use of ToString.

Dim Done As Boolean = False
Dim Inp As String
Do

   outputBlock.Text &= "Enter a real number: "
   Inp = Console.ReadLine()
   Try
      D = Double.Parse(Inp)
      outputBlock.Text &= "You entered " + D.ToString() + "." & vbCrLf
      Done = True
   Catch E As FormatException
      outputBlock.Text &= "You did not enter a number." & vbCrLf
   Catch E As Exception
      outputBlock.Text &= "An exception occurred while parsing your response: " + E.ToString() & vbCrLf
   End Try
Loop While Not Done
bool done = false;
string inp;
do
{
   outputBlock.Text += "Enter a real number: ";
   inp = Console.ReadLine();
   try
   {
      d = Double.Parse(inp);
      outputBlock.Text += String.Format("You entered {0}.", d.ToString()) + "\n";
      done = true;
   }
   catch (FormatException)
   {
      outputBlock.Text += "You did not enter a number." + "\n";
   }
   catch (Exception e)
   {
      outputBlock.Text += String.Format("An exception occurred while parsing your response: {0}", e.ToString()) + "\n";
   }
} while (!done);

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.