Single.ToString 方法

定義

將這個執行個體的數值轉換為其相等字串表示。Converts the numeric value of this instance to its equivalent string representation.

多載

ToString(String, IFormatProvider)

使用指定的格式和特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.

ToString(String)

使用指定格式,將這個執行個體的數值轉換成它的相等字串表示。Converts the numeric value of this instance to its equivalent string representation, using the specified format.

ToString(IFormatProvider)

使用指定的特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.

ToString()

將這個執行個體的數值轉換為其相等字串表示。Converts the numeric value of this instance to its equivalent string representation.

ToString(String, IFormatProvider)

使用指定的格式和特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.

public:
 virtual System::String ^ ToString(System::String ^ format, IFormatProvider ^ provider);
public string ToString (string format, IFormatProvider provider);
public string ToString (string? format, IFormatProvider? provider);
override this.ToString : string * IFormatProvider -> string
Public Function ToString (format As String, provider As IFormatProvider) As String

參數

format
String

數值格式字串。A numeric format string.

provider
IFormatProvider

物件,提供特定文化特性格式資訊。An object that supplies culture-specific formatting information.

傳回

String

這個執行個體值的字串表示,如同 formatprovider 所指定。The string representation of the value of this instance as specified by format and provider.

實作

範例

下列範例會 Single 針對數個不同的文化特性,使用每個支援的標準數值格式規範來顯示值。The following example displays a Single value using each of the supported standard numeric format specifiers for several different cultures.

float value = 16325.62901F;
string specifier;
CultureInfo culture;

// Use standard numeric format specifiers.
specifier = "G";
culture = CultureInfo.CreateSpecificCulture("eu-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325,62901
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture));
// Displays:    16325.62901

specifier = "C";
culture = CultureInfo.CreateSpecificCulture("en-US");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    $16,325.63
culture = CultureInfo.CreateSpecificCulture("en-GB");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    £16,325.63

specifier = "E04";
culture = CultureInfo.CreateSpecificCulture("sv-SE");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 1,6326E+004   
 culture = CultureInfo.CreateSpecificCulture("en-NZ");
 Console.WriteLine(value.ToString(specifier, culture));
// Displays:    1.6326E+004   

specifier = "F";
culture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325,63
culture = CultureInfo.CreateSpecificCulture("en-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325.63

specifier = "N";
culture = CultureInfo.CreateSpecificCulture("es-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16.325,63
culture = CultureInfo.CreateSpecificCulture("fr-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16 325,63

specifier = "P";
culture = CultureInfo.InvariantCulture;
Console.WriteLine((value/10000).ToString(specifier, culture));
// Displays:    163.26 %
culture = CultureInfo.CreateSpecificCulture("ar-EG");
Console.WriteLine((value/10000).ToString(specifier, culture));
// Displays:    163.256 %
Dim value As Single = 16325.62901
Dim specifier As String
Dim culture As CultureInfo

' Use standard numeric format specifiers.
specifier = "G"
culture = CultureInfo.CreateSpecificCulture("eu-ES")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16325,62901
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture))
' Displays:    16325.62901

specifier = "C"
culture = CultureInfo.CreateSpecificCulture("en-US")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    $16,325.63
culture = CultureInfo.CreateSpecificCulture("en-GB")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    £16,325.63

specifier = "E04"
culture = CultureInfo.CreateSpecificCulture("sv-SE")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 1,6326E+004   
 culture = CultureInfo.CreateSpecificCulture("en-NZ")
 Console.WriteLine(value.ToString(specifier, culture))
' Displays:    1.6326E+004   

specifier = "F"
culture = CultureInfo.CreateSpecificCulture("fr-FR")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16325,63
culture = CultureInfo.CreateSpecificCulture("en-CA")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16325.63

specifier = "N"
culture = CultureInfo.CreateSpecificCulture("es-ES")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16.325,63
culture = CultureInfo.CreateSpecificCulture("fr-CA")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16 325,63

specifier = "P"
culture = CultureInfo.InvariantCulture
Console.WriteLine((value/10000).ToString(specifier, culture))
' Displays:    163.26 %
culture = CultureInfo.CreateSpecificCulture("ar-EG")
Console.WriteLine((value/10000).ToString(specifier, culture))
' Displays:    163.256 %

備註

方法會使用指定 ToString(String, IFormatProvider) Single 之文化特性的指定格式來格式化值。The ToString(String, IFormatProvider) method formats a Single value in a specified format of a specified culture. 如果您想要使用預設格式或文化特性設定,請使用方法的其他多載,如下所示 ToStringIf you want to use default format or culture settings, use the other overloads of the ToString method, as follows:

使用格式To use format 適用于文化特性For culture 使用多載Use the overload
預設 ( "G" ) 格式Default ("G") format 預設 (目前) 文化特性Default (current) culture ToString()
預設 ( "G" ) 格式Default ("G") format 特定文化特性A specific culture ToString(IFormatProvider)
特定格式A specific format 預設 (目前) 文化特性Default (current) culture ToString(String)

傳回值可以是 PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol 或目前實例之值的字串表示,如所指定 formatThe return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or the string representation of the value of the current instance, as specified by format.

format參數可以是任何有效的標準數值格式規範,除了 D 和 X 之外,還有任何自訂數值格式規範的組合。The format parameter can be any valid standard numeric format specifier except for D and X, as well as any combination of custom numeric format specifiers. 如果 formatnull 或空字串,則此實例的傳回值會格式化為一般數值格式規範 ( "G" ) 。If format is null or an empty string, the return value for this instance is formatted with the general numeric format specifier ("G").

.NET 提供廣泛的格式化支援,在下列格式設定主題中有更詳細的說明:.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

provider參數是 IFormatProvider 其方法會傳回物件的實作為 GetFormat NumberFormatInfoThe provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object. 通常 providerCultureInfo 物件或 NumberFormatInfo 物件。Typically, provider is a CultureInfo object or a NumberFormatInfo object. provider參數會提供用於格式化的文化特性特定資訊。The provider parameter supplies culture-specific information used in formatting. 如果 providernull ,則傳回值會使用 NumberFormatInfo 目前文化特性的物件格式化。If provider is null, the return value is formatted with the NumberFormatInfo object for the current culture.

根據預設,傳回值只會包含7位數的有效位數,但在內部維護最多9個數字。By default, the return value only contains 7 digits of precision although a maximum of 9 digits is maintained internally. 如果這個實例的值超過7位數,則會傳回 ToString PositiveInfinitySymbolNegativeInfinitySymbol 而非預期的數目。If the value of this instance has greater than 7 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. 如果您需要更精確的精確度,請 format 使用 "G9" 格式規格來指定,它一律會傳回9位數的有效位數,或 "R" (如果數位可以用該有效位數表示,則會傳回7位數)或9位數(如果數位只能以最大精確度表示)。If you require more precision, specify format with the "G9" format specification, which always returns 9 digits of precision, or "R", which returns 7 digits if the number can be represented with that precision or 9 digits if the number can only be represented with maximum precision.

另請參閱

適用於

ToString(String)

使用指定格式,將這個執行個體的數值轉換成它的相等字串表示。Converts the numeric value of this instance to its equivalent string representation, using the specified format.

public:
 System::String ^ ToString(System::String ^ format);
public string ToString (string format);
public string ToString (string? format);
override this.ToString : string -> string
Public Function ToString (format As String) As String

參數

format
String

數值格式字串。A numeric format string.

傳回

String

這個執行個體值的字串表示,如同 format 所指定。The string representation of the value of this instance as specified by format.

例外狀況

format 無效。format is invalid.

範例

下列範例會定義一個數值,並將其格式化為貨幣值,方法是使用 "C" 標準數值格式字串,並使用 "N" 標準數值格式字串,將其格式化為三個小數位數的數位值。The following example defines a numeric value and formats it as a currency value by using the "C" standard numeric format string and as a numeric value to three decimal places by using the "N" standard numeric format string. 結果字串會使用 en-us 文化特性的慣例來格式化。The result strings are formatted by using the conventions of the en-US culture. 如需數值格式字串的詳細資訊,請參閱 標準數值格式字串自訂數值格式字串For more information on numeric format strings, see Standard Numeric Format Strings and Custom Numeric Format Strings.

using System;

public class Example
{
   public static void Main()
   {
      Double number = 1764.3789;
      
      // Format as a currency value.
      Console.WriteLine(number.ToString("C"));
      
      // Format as a numeric value with 3 decimal places.
      Console.WriteLine(number.ToString("N3"));
   }
}
// The example displays the following output:
//       $1,764.38
//       1,764.379
Module Example
   Public Sub Main()
      Dim number As Single = 1764.3789
      
      ' Format as a currency value.
      Console.WriteLine(number.ToString("C"))
      
      ' Format as a numeric value with 3 decimal places.
      Console.WriteLine(number.ToString("N3"))
   End Sub
End Module
' The example displays the following output:
'       $1,764.38
'       1,764.379

下列範例會 Single 使用每個支援的標準數值格式規範,以及兩個自訂數值格式字串,顯示數個值。The following example displays several Single values using each of the supported standard numeric format specifiers together with two custom numeric format strings. 其中一個自訂格式字串說明如何以 Single 前置零填補值。One of those custom format strings illustrates how to pad a Single value with leading zeros. 在將數值轉換成字串時,此範例會使用 en-us 文化特性的格式化慣例。In converting the numeric values to strings, the example uses the formatting conventions of the en-US culture.

float[] numbers= { 1054.32179F, -195489100.8377F, 1.0437E21F, 
                   -1.0573e-05F };
string[] specifiers = { "C", "E", "e", "F", "G", "N", "P", 
                        "R", "#,000.000", "0.###E-000",
                        "000,000,000,000.00###" };

foreach (float number in numbers)
{
   Console.WriteLine("Formatting of {0}:", number);
   foreach (string specifier in specifiers)
      Console.WriteLine("   {0,5}: {1}", 
                        specifier, number.ToString(specifier));

   Console.WriteLine();
}
// The example displays the following output to the console:
//       Formatting of 1054.32179:
//              C: $1,054.32
//              E: 1.054322E+003
//              e: 1.054322e+003
//              F: 1054.32
//              G: 1054.32179
//              N: 1,054.32
//              P: 105,432.18 %
//              R: 1054.32179
//          #,000.000: 1,054.322
//          0.###E-000: 1.054E003
//          000,000,000,000.00###: 000,000,001,054.322
//       
//       Formatting of -195489100.8377:
//              C: ($195,489,100.84)
//              E: -1.954891E+008
//              e: -1.954891e+008
//              F: -195489100.84
//              G: -195489100.8377
//              N: -195,489,100.84
//              P: -19,548,910,083.77 %
//              R: -195489100.8377
//          #,000.000: -195,489,100.838
//          0.###E-000: -1.955E008
//          000,000,000,000.00###: -000,195,489,100.00
//       
//       Formatting of 1.0437E+21:
//              C: $1,043,700,000,000,000,000,000.00
//              E: 1.043700E+021
//              e: 1.043700e+021
//              F: 1043700000000000000000.00
//              G: 1.0437E+21
//              N: 1,043,700,000,000,000,000,000.00
//              P: 104,370,000,000,000,000,000,000.00 %
//              R: 1.0437E+21
//          #,000.000: 1,043,700,000,000,000,000,000.000
//          0.###E-000: 1.044E021
//          000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
//       
//       Formatting of -1.0573E-05:
//              C: $0.00
//              E: -1.057300E-005
//              e: -1.057300e-005
//              F: 0.00
//              G: -1.0573E-05
//              N: 0.00
//              P: 0.00 %
//              R: -1.0573E-05
//          #,000.000: 000.000
//          0.###E-000: -1.057E-005
//          000,000,000,000.00###: -000,000,000,000.00001 
Dim numbers() As Single = {1054.32179, -195489100.8377, 1.0437E21, _
                           -1.0573e-05}
Dim specifiers() As String = { "C", "E", "e", "F", "G", "N", "P", _
                               "R", "#,000.000", "0.###E-000", _
                               "000,000,000,000.00###"}
For Each number As Single In numbers
   Console.WriteLine("Formatting of {0}:", number)
   For Each specifier As String In specifiers
      Console.WriteLine("   {0,5}: {1}", _
                        specifier, number.ToString(specifier))
   Next
   Console.WriteLine()
Next
' The example displays the following output to the console:
'       Formatting of 1054.32179:
'              C: $1,054.32
'              E: 1.054322E+003
'              e: 1.054322e+003
'              F: 1054.32
'              G: 1054.32179
'              N: 1,054.32
'              P: 105,432.18 %
'              R: 1054.32179
'          #,000.000: 1,054.322
'          0.###E-000: 1.054E003
'          000,000,000,000.00###: 000,000,001,054.322      
'       
'       Formatting of -195489100.8377:
'              C: ($195,489,100.84)
'              E: -1.954891E+008
'              e: -1.954891e+008
'              F: -195489100.84
'              G: -195489100.8377
'              N: -195,489,100.84
'              P: -19,548,910,083.77 %
'              R: -195489100.8377
'          #,000.000: -195,489,100.838
'          0.###E-000: -1.955E008
'          000,000,000,000.00###: -000,195,489,100.00
'       
'       Formatting of 1.0437E+21:
'              C: $1,043,700,000,000,000,000,000.00
'              E: 1.043700E+021
'              e: 1.043700e+021
'              F: 1043700000000000000000.00
'              G: 1.0437E+21
'              N: 1,043,700,000,000,000,000,000.00
'              P: 104,370,000,000,000,000,000,000.00 %
'              R: 1.0437E+21
'          #,000.000: 1,043,700,000,000,000,000,000.000
'          0.###E-000: 1.044E021
'          000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
'       
'       Formatting of -1.0573E-05:
'              C: $0.00
'              E: -1.057300E-005
'              e: -1.057300e-005
'              F: 0.00
'              G: -1.0573E-05
'              N: 0.00
'              P: 0.00 %
'              R: -1.0573E-05
'          #,000.000: 000.000
'          0.###E-000: -1.057E-005
'          000,000,000,000.00###: -000,000,000,000.00001

備註

ToString(String)方法會 Single 使用目前文化特性的慣例,將指定格式的值格式化。The ToString(String) method formats a Single value in a specified format by using the conventions of the current culture. 如果您想要使用預設 ( "G" 或一般) 格式,或指定不同的文化特性,請使用方法的其他多載,如下所示 ToStringIf you want to use the default ("G", or general) format or specify a different culture, use the other overloads of the ToString method, as follows:

使用格式To use format 適用于文化特性For culture 使用多載Use the overload
預設 ( "G" ) 格式Default ("G") format 預設 (目前) 文化特性Default (current) culture ToString()
預設 ( "G" ) 格式Default ("G") format 特定文化特性A specific culture ToString(IFormatProvider)
特定格式A specific format 特定文化特性A specific culture ToString(String, IFormatProvider)

傳回值可以是 PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol 或目前實例之值的字串表示,如所指定 formatThe return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or the string representation of the value of the current instance, as specified by format.

format參數可以是任何有效的標準數值格式規範,除了 D 和 X 之外,還有任何自訂數值格式規範的組合。The format parameter can be any valid standard numeric format specifier except for D and X, as well as any combination of custom numeric format specifiers. 如果 format 為 null 或空字串,則傳回值會格式化為一般數值格式規範 ( "G" ) 。If format is null or an empty string, the return value is formatted with the general numeric format specifier ("G").

.NET 提供廣泛的格式化支援,在下列格式設定主題中有更詳細的說明:.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

根據預設,傳回值只會包含7位數的有效位數,但在內部維護最多9個數字。By default, the return value only contains 7 digits of precision although a maximum of 9 digits is maintained internally. 如果這個實例的值超過7位數,則會傳回 ToString(String) PositiveInfinitySymbolNegativeInfinitySymbol 而非預期的數目。If the value of this instance has greater than 7 digits, ToString(String) returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. 如果您需要更精確的精確度,請 format 使用 "G9" 格式規格來指定,它一律會傳回9位數的有效位數,或 "R" (如果數位可以用該有效位數表示,則會傳回7位數)或9位數(如果數位只能以最大精確度表示)。If you require more precision, specify format with the "G9" format specification, which always returns 9 digits of precision, or "R", which returns 7 digits if the number can be represented with that precision or 9 digits if the number can only be represented with maximum precision.

另請參閱

適用於

ToString(IFormatProvider)

使用指定的特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.

public:
 virtual System::String ^ ToString(IFormatProvider ^ provider);
public:
 System::String ^ ToString(IFormatProvider ^ provider);
public string ToString (IFormatProvider provider);
public string ToString (IFormatProvider? provider);
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String

參數

provider
IFormatProvider

物件,提供特定文化特性格式資訊。An object that supplies culture-specific formatting information.

傳回

String

這個執行個體值的字串表示,如同 provider 所指定。The string representation of the value of this instance as specified by provider.

實作

範例

下列範例 Single CultureInfo 會使用代表數個不同文化特性的物件,顯示兩個值的字串表示。The following example displays the string representation of two Single values using CultureInfo objects that represent several different cultures.

float value;

value = -16325.62015F;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));

value = 16034.125E21F;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));
// This example displays the following output to the console:
//       -16325.62015
//       -16325.62015
//       -16325,62015
//       1.6034125E+25
//       1.6034125E+25
//       1,6034125E+25
Dim value As Single 

value = -16325.62015
' Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture))
' Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")))
' Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")))

value = 16034.125E21
' Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture))
' Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")))
' Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")))
' This example displays the following output to the console:
'       -16325.62015
'       -16325.62015
'       -16325,62015
'       1.6034125E+25
'       1.6034125E+25
'       1,6034125E+25

備註

方法會將 ToString(IFormatProvider) Single 預設 ( "G" 或指定文化特性的一般) 格式的值格式化。The ToString(IFormatProvider) method formats a Single value in the default ("G", or general) format of a specified culture. 如果您想要指定不同的格式或目前的文化特性,請使用方法的其他多載,如下所示 ToStringIf you want to specify a different format or the current culture, use the other overloads of the ToString method, as follows:

使用格式To use format 適用于文化特性For culture 使用多載Use the overload
預設 ( "G" ) 格式Default ("G") format 預設 (目前) 文化特性Default (current) culture ToString()
特定格式A specific format 預設 (目前) 文化特性Default (current) culture ToString(String)
特定格式A specific format 特定文化特性A specific culture ToString(String, IFormatProvider)

傳回值可以是 PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol 或格式的字串:The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:

[sign] 整數位數 [. [小數位數]] [e [sign] 指數位數][sign]integral-digits[.[fractional-digits]][e[sign]exponential-digits]

選擇性元素會以方括弧括住, ( [和] ) 。Optional elements are framed in square brackets ([ and ]). 包含「位數」一詞的元素包含一連串數位字元,範圍從0到9。Elements containing the term "digits" consist of a series of numeric characters ranging from 0 to 9. 下表列出每個元素。The following table lists each element.

元素Element 描述Description
Signsign 負號或正號符號。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.
pci-e'e' 小寫字元 ' e ',指出指數 (科學) 標記法。A lowercase character 'e', indicating exponential (scientific) notation.
指數位數exponential-digits 指定指數的一系列數位。A series of digits specifying an exponent.

傳回值的一些範例包括 "100"、"-123456789"、"123.45 e + 6"、"500"、"3.1416"、"600"、"-0.123" 和 "-無限大"。Some examples of the return value are "100", "-123,456,789", "123.45e+6", "500", "3.1416", "600", "-0.123", and "-Infinity".

.NET 提供廣泛的格式化支援,在下列格式設定主題中有更詳細的說明:.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

provider參數是 IFormatProvider 其方法會傳回物件的實作為 GetFormat NumberFormatInfoThe provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object. 通常 providerCultureInfo 物件或 NumberFormatInfo 物件。Typically, provider is a CultureInfo object or a NumberFormatInfo object. provider參數會提供用於格式化的文化特性特定資訊。The provider parameter supplies culture-specific information used in formatting. 如果 providernull ,則會使用目前文化特性的 NumberFormatInfo 資料來格式化傳回值。If provider is null, the return value is formatted using the NumberFormatInfo data for the current culture.

若要 Single 使用指定的文化特性和特定的格式字串,將值轉換成其字串表示,請呼叫 Single.ToString(String, IFormatProvider) 方法。To convert a Single value to its string representation using a specified culture and a specific format string, call the Single.ToString(String, IFormatProvider) method.

另請參閱

適用於

ToString()

將這個執行個體的數值轉換為其相等字串表示。Converts the numeric value of this instance to its equivalent string representation.

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

傳回

String

這個執行個體值的字串表示。The string representation of the value of this instance.

範例

下列範例會使用預設 Single.ToString 方法來顯示一些值的字串表示 SingleThe following example uses the default Single.ToString method to display the string representations of a number of Single values.

float number;

number = 1.6E20F;
// Displays 1.6E+20.
Console.WriteLine(number.ToString());

number = 1.6E2F;
// Displays 160.
Console.WriteLine(number.ToString());

number = -3.541F;
// Displays -3.541.
Console.WriteLine(number.ToString());

number = -1502345222199E-07F;
// Displays -150234.5222199.
Console.WriteLine(number.ToString());

number = -15023452221990199574E-09F;
// Displays -15023452221.9902.
Console.WriteLine(number.ToString());

number = .60344F;
// Displays 0.60344.
Console.WriteLine(number.ToString());

number = .000000001F;
// Displays 1E-09.
Console.WriteLine(number.ToString());
Dim number As Single

number = 1.6E20
' Displays 1.6E+20.      
Console.WriteLine(number.ToString())

number = 1.6E2
' Displays 160.
Console.WriteLine(number.ToString())

number = -3.541
' Displays -3.541.
Console.WriteLine(number.ToString())

number = -1502345222199E-07
' Displays -150234.5222199.
Console.WriteLine(number.ToString())

number = -15023452221990199574E-09
' Displays -15023452221.9902.
Console.WriteLine(number.ToString())

number = .60344
' Displays 0.60344.
Console.WriteLine(number.ToString())

number = .000000001
' Displays 1E-09.
Console.WriteLine(number.ToString())

下列程式碼範例說明如何 Parse(String) 搭配方法使用方法 ToString()The following code example illustrates the use of the Parse(String) method along with the ToString() method.

bool done = false;
String^ inp;
do
{
   Console::Write( "Enter a real number: " );
   inp = Console::ReadLine();
   try
   {
      s = Single::Parse( inp );
      Console::WriteLine( "You entered {0}.", s );
      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 );
   }
}
while (  !done );
bool done = false;
string inp;
do
{
    Console.Write("Enter a real number: ");
    inp = Console.ReadLine();
    try
    {
        s = Single.Parse(inp);
        Console.WriteLine("You entered {0}.", s.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);
Dim Done As Boolean = False
Dim Inp As String
Do

    Console.Write("Enter a real number: ")
    Inp = Console.ReadLine()
    Try
        S = Single.Parse(Inp)
        Console.WriteLine("You entered " + S.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

備註

方法會將 ToString() Single 預設 ( "G" 或目前文化特性的一般) 格式的值格式化。The ToString() method formats a Single value in the default ("G", or general) format of the current culture. 如果您想要指定不同的格式或文化特性,請使用方法的其他多載,如下所示 ToStringIf you want to specify a different format or culture, use the other overloads of the ToString method, as follows:

使用格式To use format 適用于文化特性For culture 使用多載Use the overload
預設 ( "G" ) 格式Default ("G") format 特定文化特性A specific culture ToString(IFormatProvider)
特定格式A specific format 預設 (目前) 文化特性Default (current) culture ToString(String)
特定格式A specific format 特定文化特性A specific culture ToString(String, IFormatProvider)

傳回值可以是 PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol 或格式的字串:The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:

[sign] 整數位數 [. [小數位數]] [e [sign] 指數位數][sign]integral-digits[.[fractional-digits]][e[sign]exponential-digits]

選擇性元素會以方括弧括住, ( [和] ) 。Optional elements are framed in square brackets ([ and ]). 包含「位數」一詞的元素包含一連串數位字元,範圍從0到9。Elements that contain the term "digits" consist of a series of numeric characters ranging from 0 to 9. 下表列出每個元素:The following table lists 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.
pci-e'e' 小寫字元 ' e ',指出指數 (科學) 標記法。A lowercase character 'e', indicating exponential (scientific) notation.
指數位數exponential-digits 指定指數的一系列數位。A series of digits specifying an exponent.

傳回值的一些範例包括 "100"、"-123456789"、"123.45 e + 6"、"500"、"3.1416"、"600"、"-0.123" 和 "-無限大"。Some examples of the return value are "100", "-123,456,789", "123.45e+6", "500", "3.1416", "600", "-0.123", and "-Infinity".

.NET 提供廣泛的格式化支援,在下列格式設定主題中有更詳細的說明:.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

另請參閱

適用於