Byte.ToString 方法

定義

將目前 Byte 物件的值轉換為其相等的字串表示。

多載

名稱 Description
ToString()

將目前 Byte 物件的值轉換為其相等的字串表示。

ToString(IFormatProvider)

利用指定的文化特定格式資訊,將當前 Byte 物件的數值轉換為其等效的字串表示。

ToString(String)

將目前 Byte 物件的值轉換為其等效字串表示,並使用指定格式。

ToString(String, IFormatProvider)

利用指定的格式及文化特有的格式資訊,將當前 Byte 物件的值轉換為其等效字串表示。

ToString()

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

將目前 Byte 物件的值轉換為其相等的字串表示。

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

傳回

此物件值的字串表示,由一串從0到9且無前置零的數字組成。

範例

以下範例顯示一個位元組數值陣列。 請注意 ToString() ,該方法在範例中並未明確被指定。 因此,由於使用 複合格式化 功能,F# 範例中隱含地使用 字串插值

byte[] bytes = {0, 1, 14, 168, 255};
foreach (byte byteValue in bytes)
   Console.WriteLine(byteValue);
// The example displays the following output to the console if the current
// culture is en-US:
//       0
//       1
//       14
//       168
//       255
let bytes = [| 0; 1; 14; 168; 255 |]
for byteValue in bytes do
    printfn $"{byteValue}"

// The example displays the following output to the console if the current
// culture is en-US:
//       0
//       1
//       14
//       168
//       255
Dim bytes() As Byte = {0, 1, 14, 168, 255}
For Each byteValue As Byte In Bytes
   Console.WriteLine(byteValue)
Next   
' The example displays the following output to the console if the current
' culture is en-US:
'       0
'       1
'       14
'       168
'       255

備註

回傳值以一般數值格式指定符(“G”)及 NumberFormatInfo 執行緒當前文化的物件來格式化。 要定義該值字串表示的格式 Byte ,請呼叫 該 ToString 方法。 若要定義用於建立值字串表示 Byte 的格式指定符與文化,請呼叫該 ToString 方法。

.NET 提供廣泛的格式支援,如下列格式化主題中更詳細地說明:

關於當前文化討論串的資訊,請參見 Thread.CurrentCulture

適用於

ToString(IFormatProvider)

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

利用指定的文化特定格式資訊,將當前 Byte 物件的數值轉換為其等效的字串表示。

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

物件,提供特定文化特性的格式資訊。

傳回

此物件值的字串表示,依參數 provider 指定格式表示。

實作

範例

以下範例會迭代一組位元組值,並透過呼叫 ToString(IFormatProvider) 不同格式提供者的方法,將每個位元組值顯示給主控台。

byte[] bytes = {0, 1, 14, 168, 255};
CultureInfo[] providers = {new CultureInfo("en-us"),
                           new CultureInfo("fr-fr"),
                           new CultureInfo("de-de"),
                           new CultureInfo("es-es")};
foreach (byte byteValue in bytes)
{
   foreach (CultureInfo provider in providers)
      Console.Write("{0,3} ({1})      ",
                    byteValue.ToString(provider), provider.Name);

   Console.WriteLine();
}
// The example displays the following output to the console:
//      0 (en-US)        0 (fr-FR)        0 (de-DE)        0 (es-ES)
//      1 (en-US)        1 (fr-FR)        1 (de-DE)        1 (es-ES)
//     14 (en-US)       14 (fr-FR)       14 (de-DE)       14 (es-ES)
//    168 (en-US)      168 (fr-FR)      168 (de-DE)      168 (es-ES)
//    255 (en-US)      255 (fr-FR)      255 (de-DE)      255 (es-ES)
let bytes = [| 0; 1; 14; 168; 255 |]
let providers = 
    [ CultureInfo "en-us"
      CultureInfo "fr-fr"
      CultureInfo "de-de"
      CultureInfo "es-es" ]

for byteValue in bytes do
    for provider in providers do
        printf $"{byteValue.ToString provider,3} ({provider.Name})      " 

    printfn ""

// The example displays the following output to the console:
//      0 (en-US)        0 (fr-FR)        0 (de-DE)        0 (es-ES)
//      1 (en-US)        1 (fr-FR)        1 (de-DE)        1 (es-ES)
//     14 (en-US)       14 (fr-FR)       14 (de-DE)       14 (es-ES)
//    168 (en-US)      168 (fr-FR)      168 (de-DE)      168 (es-ES)
//    255 (en-US)      255 (fr-FR)      255 (de-DE)      255 (es-ES)
Dim bytes() As Byte = {0, 1, 14, 168, 255}
Dim providers() As CultureInfo = {New CultureInfo("en-us"), _
                                  New CultureInfo("fr-fr"), _
                                  New CultureInfo("de-de"), _
                                  New CultureInfo("es-es")}
For Each byteValue As Byte In bytes
   For Each provider As CultureInfo In providers
      Console.Write("{0,3} ({1})      ", byteValue.ToString(provider), provider.Name)
   Next
   Console.WriteLine()                                        
Next
' The example displays the following output to the console:
'      0 (en-US)        0 (fr-FR)        0 (de-DE)        0 (es-ES)
'      1 (en-US)        1 (fr-FR)        1 (de-DE)        1 (es-ES)
'     14 (en-US)       14 (fr-FR)       14 (de-DE)       14 (es-ES)
'    168 (en-US)      168 (fr-FR)      168 (de-DE)      168 (es-ES)
'    255 (en-US)      255 (fr-FR)      255 (de-DE)      255 (es-ES)

備註

回傳值以一般數值格式指定符(「G」)格式化。

參數 provider 是一個實作介面的 IFormatProvider 物件。 其 GetFormat 方法回傳一個 NumberFormatInfo 物件,提供該方法回傳的字串格式的文化特定資訊。 實作 IFormatProvider 的物件可以是以下任一種:

如果 providernullNumberFormatInfo 無法從 取得 provider物件,則回傳值會使用 NumberFormatInfo 執行緒當前文化的物件格式化。 關於當前文化討論串的資訊,請參見 Thread.CurrentCulture

.NET 提供廣泛的格式支援,如下列格式化主題中更詳細地說明:

另請參閱

適用於

ToString(String)

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

將目前 Byte 物件的值轉換為其等效字串表示,並使用指定格式。

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

一個數字格式的字串。

傳回

目前物件的字串表示 Byte ,格式依照參數 format 指定的方式。

例外狀況

format 包含一個未受支持的指定詞。 支援的格式規範符列於備註區。

範例

以下範例初始化一個 Byte 值,並使用支援的標準格式字串及自訂格式字串顯示給主控台。 例如,en-US 作為當前文化。

string[] formats = {"C3", "D4", "e1", "E2", "F1", "G", "N1",
                    "P0", "X4", "0000.0000"};
byte number = 240;
foreach (string format in formats)
   Console.WriteLine("'{0}' format specifier: {1}",
                     format, number.ToString(format));

// The example displays the following output to the console if the
// current culture is en-us:
//       'C3' format specifier: $240.000
//       'D4' format specifier: 0240
//       'e1' format specifier: 2.4e+002
//       'E2' format specifier: 2.40E+002
//       'F1' format specifier: 240.0
//       'G' format specifier: 240
//       'N1' format specifier: 240.0
//       'P0' format specifier: 24,000 %
//       'X4' format specifier: 00F0
//       '0000.0000' format specifier: 0240.0000
let formats = 
    [ "C3"; "D4"; "e1"; "E2"; "F1"; "G"; "N1"
      "P0"; "X4"; "0000.0000" ]
let number = 240uy
for format in formats do
    printfn $"'{format}' format specifier: {number.ToString format}"

// The example displays the following output to the console if the
// current culture is en-us:
//       'C3' format specifier: $240.000
//       'D4' format specifier: 0240
//       'e1' format specifier: 2.4e+002
//       'E2' format specifier: 2.40E+002
//       'F1' format specifier: 240.0
//       'G' format specifier: 240
//       'N1' format specifier: 240.0
//       'P0' format specifier: 24,000 %
//       'X4' format specifier: 00F0
//       '0000.0000' format specifier: 0240.0000
Dim formats() As String = {"C3", "D4", "e1", "E2", "F1", "G", _
                           "N1", "P0", "X4", "0000.0000"}
Dim number As Byte = 240
For Each format As String In formats
   Console.WriteLine("'{0}' format specifier: {1}", _
                     format, number.ToString(format))
Next  
' The example displays the following output to the console if the
' current culture is en-us:
'       'C3' format specifier: $240.000
'       'D4' format specifier: 0240
'       'e1' format specifier: 2.4e+002
'       'E2' format specifier: 2.40E+002
'       'F1' format specifier: 240.0       
'       'G' format specifier: 240
'       'N1' format specifier: 240.0
'       'P0' format specifier: 24,000 %
'       'X4' format specifier: 00F0
'       '0000.0000' format specifier: 0240.0000

備註

format參數可以是標準或自訂的數字格式字串。 除了「R」(或「r」)之外,所有標準的數字格式字串皆被支援,所有自訂的數字格式字元亦皆可使用。 若 formatnull 為空字串(“”),則回傳值會以一般的數字格式指定符(“G”)格式化。

此函式的回傳值會以 NumberFormatInfo 執行緒當前文化的物件格式化。 關於當前文化討論串的資訊,請參見 Thread.CurrentCulture。 若要提供非當前文化的格式資訊,請呼叫 the Byte.ToString(String, IFormatProvider) method。

.NET 提供廣泛的格式支援,如下列格式化主題中更詳細地說明:

另請參閱

適用於

ToString(String, IFormatProvider)

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

利用指定的格式及文化特有的格式資訊,將當前 Byte 物件的值轉換為其等效字串表示。

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

標準或自定義數值格式字串。

provider
IFormatProvider

物件,提供特定文化特性的格式資訊。

傳回

目前物件的字串表示Byte,格式依照 和 format 參數指定的provider格式化。

實作

例外狀況

format 包含一個未受支持的指定詞。 支援的格式規範符列於備註區。

範例

以下範例使用標準的「N」格式字串及四個不同的 CultureInfo 物件,向主控台顯示位元組值的字串表示。

byte byteValue = 250;
CultureInfo[] providers = {new CultureInfo("en-us"),
                           new CultureInfo("fr-fr"),
                           new CultureInfo("es-es"),
                           new CultureInfo("de-de")};

foreach (CultureInfo provider in providers)
   Console.WriteLine("{0} ({1})",
                     byteValue.ToString("N2", provider), provider.Name);
// The example displays the following output to the console:
//       250.00 (en-US)
//       250,00 (fr-FR)
//       250,00 (es-ES)
//       250,00 (de-DE)
let byteValue = 250uy
let providers = 
    [ CultureInfo "en-us"
      CultureInfo "fr-fr"
      CultureInfo "es-es"
      CultureInfo "de-de" ]

for provider in providers do
    printfn $"""{byteValue.ToString("N2", provider)} ({provider.Name})"""

// The example displays the following output to the console:
//       250.00 (en-US)
//       250,00 (fr-FR)
//       250,00 (es-ES)
//       250,00 (de-DE)
Dim byteValue As Byte = 250
Dim providers() As CultureInfo = {New CultureInfo("en-us"), _
                                  New CultureInfo("fr-fr"), _
                                  New CultureInfo("es-es"), _
                                  New CultureInfo("de-de")} 
For Each provider As CultureInfo In providers 
   Console.WriteLine("{0} ({1})", _
                     byteValue.ToString("N2", provider), provider.Name)
Next   
' The example displays the following output to the console:
'       250.00 (en-US)
'       250,00 (fr-FR)
'       250,00 (es-ES)
'       250,00 (de-DE)

備註

ToString(String, IFormatProvider) 方法以指定文化的格式格式化值 Byte 。 若要使用當前文化的預設(「G」)格式來格式化號碼,請呼叫該 ToString() 方法。 若要使用當前文化指定的格式來格式化數字,請呼叫該 ToString(String) 方法。

format參數可以是標準或自訂的數字格式字串。 除了「R」(或「r」)之外,所有標準的數字格式字串皆被支援,所有自訂的數字格式字元亦皆可使用。 若 formatnull 空字串(“”),則此方法的回傳值會以一般數值格式化(「G」)格式化。

參數 provider 是一個實作介面的 IFormatProvider 物件。 其 GetFormat 方法回傳一個 NumberFormatInfo 物件,提供該方法回傳的字串格式的文化特定資訊。 實作 IFormatProvider 的物件可以是以下任一種:

如果 providernullNumberFormatInfo 無法從 取得 provider物件,則回傳值會使用 NumberFormatInfo 執行緒當前文化的物件格式化。 關於當前文化討論串的資訊,請參見 Thread.CurrentCulture

.NET 提供廣泛的格式支援,如下列格式化主題中更詳細地說明:

另請參閱

適用於