Freigeben über


Single.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 that contain the term "digits" consist of a series of numeric characters ranging from 0 to 9. The following table describes each element.

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".

NoteNote:

Because the Single data type is not supported on the Macintosh OS X operating system, the string representation of a Single value may be different from those of the other .NET Framework numeric types that are supported by OS X.

This overload of the ToString method implicitly uses the general numeric format specifier ("G") and the NumberFormatInfo object for the current culture.

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

Examples

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

' Example for the Single.ToString( ) methods.
Imports System.Globalization

Module Example

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim singleValue As Single = 11876.54321
      Dim format As String = "  {0,-30}{1}"

      outputBlock.Text &= String.Format("This example of" & vbCrLf & _
          "  Single.ToString( ), " & vbCrLf & _
          "  Single.ToString( String )," & vbCrLf & _
          "  Single.ToString( IFormatProvider ), and " & vbCrLf & _
          "  Single.ToString( String, IFormatProvider )" & _
          vbCrLf & "generates the following output when run in " & _
          "the [{0}] culture. " & vbCrLf & "A Single number is " & _
          "formatted with various combinations of format " & _
          "" & vbCrLf & "strings and IFormatProvider.", _
          CultureInfo.CurrentCulture.Name) & vbCrLf

      ' Format the number without and with format strings.
      outputBlock.Text &= String.Format(vbCrLf & "IFormatProvider is not " & _
          "used; the default culture is [{0}]:", _
          CultureInfo.CurrentCulture.Name) & vbCrLf
      outputBlock.Text &= String.Format(format, _
          "No format string:", singleValue.ToString()) & vbCrLf
      outputBlock.Text &= String.Format(format, _
          "'N5' format string:", singleValue.ToString("N5")) & vbCrLf
      outputBlock.Text &= String.Format(format, _
          "'E' format string:", singleValue.ToString("E")) & vbCrLf
      outputBlock.Text &= String.Format(format, _
          "'E5' format string:", singleValue.ToString("E5")) & vbCrLf

      ' Create a CultureInfo object for another culture. Use
      ' [Dutch - The Netherlands] unless the current culture
      ' is Dutch language. In that case use [English - U.S.].
      Dim cultureName As String = IIf( _
          CultureInfo.CurrentCulture.Name.Substring(0, 2) = _
              "nl", "en-US", "nl-NL")
      Dim culture As New CultureInfo(cultureName)

      ' Use the CultureInfo object for an IFormatProvider.
      outputBlock.Text &= String.Format(vbCrLf & "A CultureInfo object " & _
          "for [{0}] is used for the IFormatProvider: ", _
          cultureName) & vbCrLf
      outputBlock.Text &= String.Format(format, "No format string:", _
          singleValue.ToString(culture)) & vbCrLf
      outputBlock.Text &= String.Format(format, "'N5' format string:", _
          singleValue.ToString("N5", culture)) & vbCrLf
      outputBlock.Text &= String.Format(format, "'E' format string:", _
          singleValue.ToString("E", culture)) & vbCrLf

      ' Get the NumberFormatInfo object from CultureInfo, and
      ' then change the digit group size to 2 and the digit
      ' separator to '_'.
      Dim numInfo As NumberFormatInfo = culture.NumberFormat
      numInfo.NumberGroupSizes = New Integer() {2}
      numInfo.NumberGroupSeparator = "_"

      ' Use a NumberFormatInfo object for IFormatProvider.
      outputBlock.Text &= String.Format(vbCrLf & _
          "A NumberFormatInfo object with digit group " & _
          "size = 2 and " & vbCrLf & "digit separator " & _
          "= '_' is used for the IFormatProvider:") & vbCrLf
      outputBlock.Text &= String.Format(format, "'N' format string:", _
          singleValue.ToString("N", culture)) & vbCrLf
      outputBlock.Text &= String.Format(format, "'E' format string:", _
          singleValue.ToString("E", culture)) & vbCrLf
   End Sub
End Module

' This example of
'   Single.ToString( ),
'   Single.ToString( String ),
'   Single.ToString( IFormatProvider ), and
'   Single.ToString( String, IFormatProvider )
' generates the following output when run in the [en-US] culture.
' A Single number is formatted with various combinations of format
' strings and IFormatProvider.
' 
' IFormatProvider is not used; the default culture is [en-US]:
'   No format string:             11876.54
'   'N5' format string:           11,876.54000
'   'E' format string:            1.187654E+004
'   'E5' format string:           1.18765E+004
' 
' A CultureInfo object for [nl-NL] is used for the IFormatProvider:
'   No format string:             11876,54
'   'N5' format string:           11.876,54000
'   'E' format string:            1,187654E+004
' 
' A NumberFormatInfo object with digit group size = 2 and
' digit separator = '_' is used for the IFormatProvider:
'   'N' format string:            1_18_76,54
'   'E' format string:            1,187654E+004
// Example for the Single.ToString( ) methods.
using System;
using System.Globalization;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      float singleValue = 11876.54321F;
      string format = "  {0,-30}{1}";

      outputBlock.Text += String.Format("This example of\n" +
          "  Single.ToString( ), \n" +
          "  Single.ToString( String ),\n" +
          "  Single.ToString( IFormatProvider ), and \n" +
          "  Single.ToString( String, IFormatProvider )\n" +
          "generates the following output when run in the [{0}] " +
          "culture. \nA Single number is formatted with various " +
          "combinations of format \nstrings and IFormatProvider.",
          CultureInfo.CurrentCulture.Name) + "\n";

      // Format the number without and with format strings.
      outputBlock.Text += String.Format("\nIFormatProvider is not " +
          "used; the default culture is [{0}]:",
          CultureInfo.CurrentCulture.Name) + "\n";
      outputBlock.Text += String.Format(format, "No format string:",
          singleValue.ToString()) + "\n";
      outputBlock.Text += String.Format(format, "'N5' format string:",
          singleValue.ToString("N5")) + "\n";
      outputBlock.Text += String.Format(format, "'E' format string:",
          singleValue.ToString("E")) + "\n";
      outputBlock.Text += String.Format(format, "'E5' format string:",
          singleValue.ToString("E5")) + "\n";

      // Create a CultureInfo object for another culture. Use
      // [Dutch - The Netherlands] unless the current culture
      // is Dutch language. In that case use [English - U.S.].
      string cultureName =
          CultureInfo.CurrentCulture.Name.Substring(0, 2) == "nl" ?
              "en-US" : "nl-NL";
      CultureInfo culture = new CultureInfo(cultureName);

      // Use the CultureInfo object for an IFormatProvider.
      outputBlock.Text += String.Format("\nA CultureInfo object " +
          "for [{0}] is used for the IFormatProvider: ",
          cultureName) + "\n";
      outputBlock.Text += String.Format(format, "No format string:",
          singleValue.ToString(culture)) + "\n";
      outputBlock.Text += String.Format(format, "'N5' format string:",
          singleValue.ToString("N5", culture)) + "\n";
      outputBlock.Text += String.Format(format, "'E' format string:",
          singleValue.ToString("E", culture)) + "\n";

      // Get the NumberFormatInfo object from CultureInfo, and
      // then change the digit group size to 2 and the digit
      // separator to '_'.
      NumberFormatInfo numInfo = culture.NumberFormat;
      numInfo.NumberGroupSizes = new int[] { 2 };
      numInfo.NumberGroupSeparator = "_";

      // Use a NumberFormatInfo object for IFormatProvider.
      outputBlock.Text += String.Format("\nA NumberFormatInfo object with digit " +
          "group size = 2 and \ndigit separator = '_' is used " +
          "for the IFormatProvider:") + "\n";
      outputBlock.Text += String.Format(format, "'N' format string:",
          singleValue.ToString("N", culture)) + "\n";
      outputBlock.Text += String.Format(format, "'E' format string:",
          singleValue.ToString("E", culture)) + "\n";
   }
}

/*
This example of
  Single.ToString( ),
  Single.ToString( String ),
  Single.ToString( IFormatProvider ), and
  Single.ToString( String, IFormatProvider )
generates the following output when run in the [en-US] culture.
A Single number is formatted with various combinations of format
strings and IFormatProvider.

IFormatProvider is not used; the default culture is [en-US]:
  No format string:             11876.54
  'N5' format string:           11,876.54000
  'E' format string:            1.187654E+004
  'E5' format string:           1.18765E+004

A CultureInfo object for [nl-NL] is used for the IFormatProvider:
  No format string:             11876,54
  'N5' format string:           11.876,54000
  'E' format string:            1,187654E+004

A NumberFormatInfo object with digit group size = 2 and
digit separator = '_' is used for the IFormatProvider:
  'N' format string:            1_18_76,54
  'E' format string:            1,187654E+004
*/

The following code example illustrates the use of the Parse(String) method along with the ToString() method.

Dim Done As Boolean = False
Dim Inp As String
Do

   outputBlock.Text &= "Enter a real number: "
   Inp = Console.ReadLine()
   Try
      S = Single.Parse(Inp)
      outputBlock.Text &= "You entered " + S.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
   {
      s = Single.Parse(inp);
      outputBlock.Text += String.Format("You entered {0}.", s.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.