SByte.ToString 方法

定義

將這個執行個體的數值轉換為其相等字串表示。

多載

ToString()

將這個執行個體的數值轉換為其相等字串表示。

ToString(IFormatProvider)

使用指定的特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。

ToString(String)

使用指定格式,將這個執行個體的數值轉換成它的相等字串表示。

ToString(String, IFormatProvider)

使用指定的格式和特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。

ToString()

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

將這個執行個體的數值轉換為其相等字串表示。

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

傳回

這個執行個體值的字串表示,包含減號 (如果數值為負) 及沒有零的前置字元且範圍從 0 到 9 的數字順序所組成。

範例

下列範例會使用預設 ToString() 方法顯示 SByte 值。 它也會顯示使用一些標準格式規範所產生的值字串表示 SByte 。 這些範例會使用 en-US 文化特性的格式設定慣例來顯示。

using System;

public class Example
{
   public static void Main()
   {
      sbyte value = -123;
      // Display value using default ToString method.
      Console.WriteLine(value.ToString());            // Displays -123
      // Display value using some standard format specifiers.
      Console.WriteLine(value.ToString("G"));         // Displays -123
      Console.WriteLine(value.ToString("C"));         // Displays ($-123.00)
      Console.WriteLine(value.ToString("D"));         // Displays -123
      Console.WriteLine(value.ToString("F"));         // Displays -123.00
      Console.WriteLine(value.ToString("N"));         // Displays -123.00
      Console.WriteLine(value.ToString("X"));         // Displays 85
   }
}
let value = -123y
// Display value using default ToString method.
printfn $"{value.ToString()}"               // Displays -123
// Display value using some standard format specifiers.
printfn $"""{value.ToString "G"}"""         // Displays -123
printfn $"""{value.ToString "C"}"""         // Displays ($-123.00)
printfn $"""{value.ToString "D"}"""         // Displays -123
printfn $"""{value.ToString "F"}"""         // Displays -123.00
printfn $"""{value.ToString "N"}"""         // Displays -123.00
printfn $"""{value.ToString "X"}"""         // Displays 85
Module Example
   Public Sub Main()
      Dim value As SByte = -123
      ' Display value using default ToString method.
      Console.WriteLine(value.ToString())            ' Displays -123
      ' Display value using some standard format specifiers.
      Console.WriteLine(value.ToString("G"))         ' Displays -123
      Console.WriteLine(value.ToString("C"))         ' Displays ($-123.00)
      Console.WriteLine(value.ToString("D"))         ' Displays -123
      Console.WriteLine(value.ToString("F"))         ' Displays -123.00
      Console.WriteLine(value.ToString("N"))         ' Displays -123.00
      Console.WriteLine(value.ToString("X"))         ' Displays 85
   End Sub
End Module

備註

方法會將 ToString() 值格式化 SByte 為預設 (「G」,或目前文化特性的一般) 格式。 如果您想要指定不同的格式或文化特性,請使用 方法的其他多載 ToString ,如下所示:

使用格式 針對文化特性 使用多載
預設 (「G」) 格式 特定文化特性 ToString(IFormatProvider)
特定格式 預設 (目前) 文化特性 ToString(String)
特定格式 特定文化特性 ToString(String, IFormatProvider)

傳回值會使用一般數值格式規範來格式化, (「G」) 如果值的值為負數,則值的字串表示 SByte 會包含負號,以及介於 0 到 9 之間的數位序列,而沒有前置零。 負號是由 NumberFormatInfo 目前文化特性的物件所定義。

若要定義帶正負號位元組值的字串表示格式,請呼叫 ToString(String) 方法。

另請參閱

適用於

ToString(IFormatProvider)

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

使用指定的特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。

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 所指定。

實作

範例

下列範例會定義自訂 NumberFormatInfo 物件,並將 「~」 字元指派給其 NegativeSign 屬性。 此範例接著會使用此自訂物件以及 NumberFormatInfo 不因文化特性而異的物件來格式化一系列 SByte 值。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      // Define a custom NumberFormatInfo object with "~" as its negative sign.
      NumberFormatInfo nfi = new NumberFormatInfo();
      nfi.NegativeSign = "~";
      
      // Initialize an array of SByte values.
      sbyte[] bytes = { -122, 17, 124 };

      // Display the formatted result using the custom provider.
      Console.WriteLine("Using the custom NumberFormatInfo object:");
      foreach (sbyte value in bytes)
         Console.WriteLine(value.ToString(nfi));

      Console.WriteLine();
          
      // Display the formatted result using the invariant culture.
      Console.WriteLine("Using the invariant culture:");
      foreach (sbyte value in bytes)
         Console.WriteLine(value.ToString(NumberFormatInfo.InvariantInfo));
   }
}
// The example displays the following output:
//       Using the custom NumberFormatInfo object:
//       ~122
//       17
//       124
//       
//       Using the invariant culture:
//       -122
//       17
//       124
open System.Globalization

// Define a custom NumberFormatInfo object with "~" as its negative sign.
let nfi = NumberFormatInfo(NegativeSign = "~")

// Initialize an array of SByte values.
let bytes = [| -122y; 17y; 124y |]

// Display the formatted result using the custom provider.
printfn "Using the custom NumberFormatInfo object:"
for value in bytes do
    printfn $"{value.ToString nfi}"

printfn ""
      
// Display the formatted result using the invariant culture.
printfn "Using the invariant culture:"
for value in bytes do
   printfn $"{value.ToString NumberFormatInfo.InvariantInfo}"
// The example displays the following output:
//       Using the custom NumberFormatInfo object:
//       ~122
//       17
//       124
//       
//       Using the invariant culture:
//       -122
//       17
//       124
Imports System.Globalization

Module Example
   Public Sub Main()
      ' Define a custom NumberFormatInfo object with "~" as its negative sign.
      Dim nfi As New NumberFormatInfo()
      nfi.NegativeSign = "~"
      
      ' Initialize an array of SByte values.
      Dim bytes() As SByte = { -122, 17, 124 }

      ' Display the formatted result using the custom provider.
      Console.WriteLine("Using the custom NumberFormatInfo object:")
      For Each value As SByte In bytes
         Console.WriteLine(value.ToString(nfi))
      Next
      Console.WriteLine()
          
      ' Display the formatted result using the invariant culture.
      Console.WriteLine("Using the invariant culture:")
      For Each value As SByte In bytes
         Console.WriteLine(value.ToString(NumberFormatInfo.InvariantInfo))
      Next   
   End Sub
End Module
' The example displays the following output:
'       Using the custom NumberFormatInfo object:
'       ~122
'       17
'       124
'       
'       Using the invariant culture:
'       -122
'       17
'       124

備註

方法會將 ToString(IFormatProvider) 值格式化 SByte 為預設 (「G」,或指定文化特性的一般) 格式。 如果您想要指定不同的格式或目前的文化特性,請使用 方法的其他多載 ToString ,如下所示:

使用格式 針對文化特性 使用多載
預設 (「G」) 格式 預設 (目前) 文化特性 ToString()
特定格式 預設 (目前) 文化特性 ToString(String)
特定格式 特定文化特性 ToString(String, IFormatProvider)

參數 provider 是實作 IFormatProvider 。 其 GetFormat 方法會傳 NumberFormatInfo 回 物件,提供這個方法所傳回字串格式的特定文化特性資訊。 如果 為 providernull ,則會 SByte 使用 NumberFormatInfo 目前文化特性的物件來格式化值。 使用一般格式規範控制值字串表示 SByte 之物件的唯一屬性 NumberFormatInfoNumberFormatInfo.NegativeSign ,它會定義代表負號的字元。

參數 provider 可以是下列其中一項:

另請參閱

適用於

ToString(String)

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

使用指定格式,將這個執行個體的數值轉換成它的相等字串表示。

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

標準或自訂數值格式字串。

傳回

這個執行個體值的字串表示,如同 format 所指定。

例外狀況

format 無效。

範例

下列範例會初始化值的陣列 SByte ,並使用每個標準格式字串和一些自訂格式字串加以顯示。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      sbyte[] values = { -124, 0, 118 };
      string[] specifiers = { "G", "C", "D3", "E2", "e3", "F", 
                              "N", "P", "X", "00.0", "#.0", 
                              "000;(0);**Zero**" };
      
      foreach (sbyte value in values)
      {
         foreach (string specifier in specifiers)
            Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//       G: -124
//       C: ($124.00)
//       D3: -124
//       E2: -1.24E+002
//       e3: -1.240e+002
//       F: -124.00
//       N: -124.00
//       P: -12,400.00 %
//       X: 84
//       00.0: -124.0
//       #.0: -124.0
//       000;(0);**Zero**: (124)
//       
//       G: 0
//       C: $0.00
//       D3: 000
//       E2: 0.00E+000
//       e3: 0.000e+000
//       F: 0.00
//       N: 0.00
//       P: 0.00 %
//       X: 0
//       00.0: 00.0
//       #.0: .0
//       000;(0);**Zero**: **Zero**
//       
//       G: 118
//       C: $118.00
//       D3: 118
//       E2: 1.18E+002
//       e3: 1.180e+002
//       F: 118.00
//       N: 118.00
//       P: 11,800.00 %
//       X: 76
//       00.0: 118.0
//       #.0: 118.0
//       000;(0);**Zero**: 118
let values = [| -124y; 0y; 118y |]
let specifiers = 
    [| "G"; "C"; "D3"; "E2"; "e3"; "F" 
       "N"; "P"; "X"; "00.0"; "#.0" 
       "000(0)**Zero**" |]

for value in values do
    for specifier in specifiers do
        printfn $"{specifier}: {value.ToString specifier}"
    printfn ""
// The example displays the following output:
//       G: -124
//       C: ($124.00)
//       D3: -124
//       E2: -1.24E+002
//       e3: -1.240e+002
//       F: -124.00
//       N: -124.00
//       P: -12,400.00 %
//       X: 84
//       00.0: -124.0
//       #.0: -124.0
//       000(0)**Zero**: (124)
//       
//       G: 0
//       C: $0.00
//       D3: 000
//       E2: 0.00E+000
//       e3: 0.000e+000
//       F: 0.00
//       N: 0.00
//       P: 0.00 %
//       X: 0
//       00.0: 00.0
//       #.0: .0
//       000(0)**Zero**: **Zero**
//       
//       G: 118
//       C: $118.00
//       D3: 118
//       E2: 1.18E+002
//       e3: 1.180e+002
//       F: 118.00
//       N: 118.00
//       P: 11,800.00 %
//       X: 76
//       00.0: 118.0
//       #.0: 118.0
//       000(0)**Zero**: 118
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim values() As SByte = { -124, 0, 118 }
      Dim specifiers() As String = { "G", "C", "D3", "E2", "e3", "F", _
                                     "N", "P", "X", "00.0", "#.0", _
                                     "000;(0);**Zero**" }
      
      For Each value As SByte In values
         For Each specifier As String In specifiers
            Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
         Next
         Console.WriteLine()
      Next
   End Sub
End Module
' The example displays the following output:
'       G: -124
'       C: ($124.00)
'       D3: -124
'       E2: -1.24E+002
'       e3: -1.240e+002
'       F: -124.00
'       N: -124.00
'       P: -12,400.00 %
'       X: 84
'       00.0: -124.0
'       #.0: -124.0
'       000;(0);**Zero**: (124)
'       
'       G: 0
'       C: $0.00
'       D3: 000
'       E2: 0.00E+000
'       e3: 0.000e+000
'       F: 0.00
'       N: 0.00
'       P: 0.00 %
'       X: 0
'       00.0: 00.0
'       #.0: .0
'       000;(0);**Zero**: **Zero**
'       
'       G: 118
'       C: $118.00
'       D3: 118
'       E2: 1.18E+002
'       e3: 1.180e+002
'       F: 118.00
'       N: 118.00
'       P: 11,800.00 %
'       X: 76
'       00.0: 118.0
'       #.0: 118.0
'       000;(0);**Zero**: 118

備註

方法 ToString(String) 會使用目前文化特性的慣例,以指定格式格式化 SByte 值。 如果您想要使用預設 (「G」,或一般) 格式或指定不同的文化特性,請使用 方法的其他多載 ToString ,如下所示:

使用格式 針對文化特性 使用多載
預設 (「G」) 格式 預設 (目前) 文化特性 ToString()
預設 (「G」) 格式 特定文化特性 ToString(IFormatProvider)
特定格式 特定文化特性 ToString(String, IFormatProvider)

參數 format 可以是任何有效的標準數值格式規範,或自訂數值格式規範的任何組合。 如果 format 等於 String.Empty 或 為 null ,則目前物件的傳回值會以一 SByte 般格式標準格式化 (「G」) 。 如果 format 是任何其他值,方法會 FormatException 擲回 。

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

傳回字串的格式是由 NumberFormatInfo 目前文化特性的物件所決定。 根據 format 參數,這個物件會控制輸出字串中的負號、群組分隔符號和小數點符號等符號。 若要為目前文化特性以外的文化特性提供格式資訊,請呼叫 ToString(String, IFormatProvider) 多載。

另請參閱

適用於

ToString(String, IFormatProvider)

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

使用指定的格式和特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。

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

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

傳回

這個執行個體值的字串表示,如同 formatprovider 所指定。

實作

例外狀況

format 無效。

範例

下列範例會使用標準數值格式規範和一些特定 CultureInfo 物件來顯示正值和負 SByte 值。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      // Define cultures whose formatting conventions are to be used.
      CultureInfo[] cultures = { CultureInfo.CreateSpecificCulture("en-US"), 
                                 CultureInfo.CreateSpecificCulture("fr-FR"), 
                                 CultureInfo.CreateSpecificCulture("es-ES") };
      sbyte positiveNumber = 119;
      sbyte negativeNumber = -45;
      string[] specifiers = {"G", "C", "D4", "E2", "F", "N", "P", "X2"}; 
      
      foreach (string specifier in specifiers)
      {
         foreach (CultureInfo culture in cultures)
            Console.WriteLine("{0,2} format using {1} culture: {2, 16} {3, 16}",  
                              specifier, culture.Name, 
                              positiveNumber.ToString(specifier, culture), 
                              negativeNumber.ToString(specifier, culture));
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//     G format using en-US culture:              119              -45
//     G format using fr-FR culture:              119              -45
//     G format using es-ES culture:              119              -45
//    
//     C format using en-US culture:          $119.00         ($45.00)
//     C format using fr-FR culture:         119,00 €         -45,00 €
//     C format using es-ES culture:         119,00 €         -45,00 €
//    
//    D4 format using en-US culture:             0119            -0045
//    D4 format using fr-FR culture:             0119            -0045
//    D4 format using es-ES culture:             0119            -0045
//    
//    E2 format using en-US culture:        1.19E+002       -4.50E+001
//    E2 format using fr-FR culture:        1,19E+002       -4,50E+001
//    E2 format using es-ES culture:        1,19E+002       -4,50E+001
//    
//     F format using en-US culture:           119.00           -45.00
//     F format using fr-FR culture:           119,00           -45,00
//     F format using es-ES culture:           119,00           -45,00
//    
//     N format using en-US culture:           119.00           -45.00
//     N format using fr-FR culture:           119,00           -45,00
//     N format using es-ES culture:           119,00           -45,00
//    
//     P format using en-US culture:      11,900.00 %      -4,500.00 %
//     P format using fr-FR culture:      11 900,00 %      -4 500,00 %
//     P format using es-ES culture:      11.900,00 %      -4.500,00 %
//    
//    X2 format using en-US culture:               77               D3
//    X2 format using fr-FR culture:               77               D3
//    X2 format using es-ES culture:               77               D3
open System.Globalization

// Define cultures whose formatting conventions are to be used.
let cultures = 
    [| CultureInfo.CreateSpecificCulture "en-US" 
       CultureInfo.CreateSpecificCulture "fr-FR" 
       CultureInfo.CreateSpecificCulture "es-ES" |]
let positiveNumber = 119y
let negativeNumber = -45y
let specifiers = [| "G"; "C"; "D4"; "E2"; "F"; "N"; "P"; "X2" |]

for specifier in specifiers do
    for culture in cultures do
        printfn $"{specifier,2} format using {culture.Name} culture: {positiveNumber.ToString(specifier, culture), 16} {negativeNumber.ToString(specifier, culture), 16}"
    printfn ""
// The example displays the following output:
//     G format using en-US culture:              119              -45
//     G format using fr-FR culture:              119              -45
//     G format using es-ES culture:              119              -45
//    
//     C format using en-US culture:          $119.00         ($45.00)
//     C format using fr-FR culture:         119,00 €         -45,00 €
//     C format using es-ES culture:         119,00 €         -45,00 €
//    
//    D4 format using en-US culture:             0119            -0045
//    D4 format using fr-FR culture:             0119            -0045
//    D4 format using es-ES culture:             0119            -0045
//    
//    E2 format using en-US culture:        1.19E+002       -4.50E+001
//    E2 format using fr-FR culture:        1,19E+002       -4,50E+001
//    E2 format using es-ES culture:        1,19E+002       -4,50E+001
//    
//     F format using en-US culture:           119.00           -45.00
//     F format using fr-FR culture:           119,00           -45,00
//     F format using es-ES culture:           119,00           -45,00
//    
//     N format using en-US culture:           119.00           -45.00
//     N format using fr-FR culture:           119,00           -45,00
//     N format using es-ES culture:           119,00           -45,00
//    
//     P format using en-US culture:      11,900.00 %      -4,500.00 %
//     P format using fr-FR culture:      11 900,00 %      -4 500,00 %
//     P format using es-ES culture:      11.900,00 %      -4.500,00 %
//    
//    X2 format using en-US culture:               77               D3
//    X2 format using fr-FR culture:               77               D3
//    X2 format using es-ES culture:               77               D3
Imports System.Globalization

Module Example
   Public Sub Main()
      ' Define cultures whose formatting conventions are to be used.
      Dim cultures() As CultureInfo = {CultureInfo.CreateSpecificCulture("en-US"), _
                                       CultureInfo.CreateSpecificCulture("fr-FR"), _
                                       CultureInfo.CreateSpecificCulture("es-ES") }
      Dim positiveNumber As SByte = 119
      Dim negativeNumber As SByte = -45
      Dim specifiers() As String = {"G", "C", "D4", "E2", "F", "N", "P", "X2"} 
      
      For Each specifier As String In specifiers
         For Each culture As CultureInfo In Cultures
            Console.WriteLine("{0,2} format using {1} culture: {2, 16} {3, 16}", _ 
                              specifier, culture.Name, _
                              positiveNumber.ToString(specifier, culture), _
                              negativeNumber.ToString(specifier, culture))

         Next
         Console.WriteLine()
      Next
   End Sub
End Module
' The example displays the following output:
'     G format using en-US culture:              119              -45
'     G format using fr-FR culture:              119              -45
'     G format using es-ES culture:              119              -45
'    
'     C format using en-US culture:          $119.00         ($45.00)
'     C format using fr-FR culture:         119,00 €         -45,00 €
'     C format using es-ES culture:         119,00 €         -45,00 €
'    
'    D4 format using en-US culture:             0119            -0045
'    D4 format using fr-FR culture:             0119            -0045
'    D4 format using es-ES culture:             0119            -0045
'    
'    E2 format using en-US culture:        1.19E+002       -4.50E+001
'    E2 format using fr-FR culture:        1,19E+002       -4,50E+001
'    E2 format using es-ES culture:        1,19E+002       -4,50E+001
'    
'     F format using en-US culture:           119.00           -45.00
'     F format using fr-FR culture:           119,00           -45,00
'     F format using es-ES culture:           119,00           -45,00
'    
'     N format using en-US culture:           119.00           -45.00
'     N format using fr-FR culture:           119,00           -45,00
'     N format using es-ES culture:           119,00           -45,00
'    
'     P format using en-US culture:      11,900.00 %      -4,500.00 %
'     P format using fr-FR culture:      11 900,00 %      -4 500,00 %
'     P format using es-ES culture:      11.900,00 %      -4.500,00 %
'    
'    X2 format using en-US culture:               77               D3
'    X2 format using fr-FR culture:               77               D3
'    X2 format using es-ES culture:               77               D3

備註

方法會 ToString(String, IFormatProvider) 以指定文化特性的指定格式格式化 SByte 值。 如果您想要使用預設格式或文化特性設定,請使用 方法的其他多載 ToString ,如下所示:

使用格式 針對文化特性 使用多載
預設 (「G」) 格式 預設 (目前) 文化特性 ToString()
預設 (「G」) 格式 特定文化特性 ToString(IFormatProvider)
特定格式 預設 (目前) 文化特性 ToString(String)

參數 format 可以是任何有效的標準數值格式規範,或自訂數值格式規範的任何組合。 如果 format 等於 String.Empty 或 為 null ,則目前 SByte 物件的傳回值會以一般格式標準格式化, (「G」) 。 如果 format 為任何其他值,方法會擲回 FormatException

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

參數 provider 是實作 IFormatProvider 。 其 GetFormat 方法會 NumberFormatInfo 傳回 物件,提供這個方法所傳回之字串格式的文化特性特定資訊。 ToString(String, IFormatProvider)叫用 方法時,它會呼叫 provider 參數的 方法,並傳遞 Type 代表 NumberFormatInfo 型別的 IFormatProvider.GetFormat 物件。 GetFormat然後,方法會 NumberFormatInfo 傳回 物件,提供格式化 value 參數的資訊,例如負號符號、群組分隔符號或小數點符號。 有三種方式可以使用 provider 參數,將格式資訊 ToString(String, IFormatProvider) 提供給 方法:

如果 providernull ,則傳回字串的格式設定是以目前文化特性的物件為基礎 NumberFormatInfo

另請參閱

適用於