信息
您所需的主题如下所示。但此主题未包含在此库中。

Single.ToString 方法

2013/12/13

将此实例的数值转换为其等效的字符串表示形式。

Namespace:  System
程序集:  mscorlib(位于 mscorlib.dll 中)

public override string ToString()

返回值

类型: System.String
此实例的值的字符串表示形式。

返回值可以是 PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol 或如下形式的字符串:

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

可选元素括在方括号([ 和 ])中。包含术语“digits”的元素由一系列从 0 到 9 的数字字符组成。下表对每个元素进行了介绍。

元素

说明

sign

负号或正号。

integral-digits

指定数字整数部分的一系列数字。有小数位时可以没有整数位。

'.'

区域性特定的小数点符号。

fractional-digits

指定数字小数部分的一系列数字。

“e”

小写字符“e”,指示指数(科学型)表示法。

exponential-digits

指定指数的一系列数字。

返回值的示例包括“100”、“-123,456,789”、“123.45e+6”、“500”、“3.1416”、“600”、“-0.123”和“负无穷大”。

ToString 方法重载隐式使用通用数值格式说明符(“G”)和当前区域性的 NumberFormatInfo 对象。

下面的示例使用默认的 Single.ToString 方法显示许多 Single 值的字符串表示形式。


// 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
*/


下面的代码示例演示如何使用 Parse(String) 方法以及 ToString() 方法。


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


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

显示: