匯出 (0) 列印
全部展開

Single.ToString Method

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

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

[SecuritySafeCriticalAttribute]
public override string ToString()

Return Value

Type: System.String
The string representation of the value of this instance.

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:

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


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);


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

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

社群新增項目

新增
顯示:
© 2014 Microsoft