SByte.ToString 方法

定义

将此实例的数值转换为其等效的字符串表示形式。Converts the numeric value of this instance to its equivalent string representation.

重载

ToString(IFormatProvider)

使用指定的区域性特定格式信息,将此实例的数值转换为它的等效字符串表示形式。Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.

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

将此实例的数值转换为其等效的字符串表示形式。Converts the numeric value of this instance to its equivalent string representation.

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.

实现

示例

下面的示例定义了一个自定义 NumberFormatInfo 对象,并将 "~" 字符分配到其 NegativeSign 属性。The following example defines a custom NumberFormatInfo object and assigns the "~" character to its NegativeSign property. 然后,该示例使用此自定义对象和 NumberFormatInfo 固定区域性的对象来设置一系列值的格式 SByteThe example then uses this custom object as well as the NumberFormatInfo object of the invariant culture to format a series of SByte values.

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
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" 中的值的格式设置为指定区域性的常规) 格式。The ToString(IFormatProvider) method formats an SByte 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)

provider参数是一个 IFormatProvider 实现。The provider parameter is an IFormatProvider implementation. GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关此方法返回的字符串格式的区域性特定信息。Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of the string returned by this method. 如果 providernull ,则 SByte 使用当前区域性的对象设置值的格式 NumberFormatInfoIf provider is null, the SByte value is formatted using the NumberFormatInfo object of the current culture. NumberFormatInfo使用常规格式说明符控制值的字符串表示形式的对象的唯一属性 SByteNumberFormatInfo.NegativeSign ,该属性定义表示负号的字符。The only property of the NumberFormatInfo object that controls the string representation of the SByte value using the general format specifier is NumberFormatInfo.NegativeSign, which defines the character that represents the negative sign.

provider参数可以是以下项之一:The provider parameter can be one of the following:

另请参阅

适用于

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 standard or custom 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.

实现

例外

format 无效。format is invalid.

示例

下面的示例 SByte 使用标准数值格式说明符和多个特定对象显示正值和负值 CultureInfoThe following example displays both a positive and a negative SByte value using the standard numeric format specifiers and a number of specific CultureInfo objects.

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
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 的区域性的指定格式设置值的格式。The ToString(String, IFormatProvider) method formats an SByte value in a specified format of a specified culture. 如果要使用默认格式或区域性设置,请使用方法的其他重载 ToString ,如下所示:If 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)

format参数可以是任何有效的标准数值格式说明符,也可以是自定义数字格式说明符的任意组合。The format parameter can be any valid standard numeric format specifier, or any combination of custom numeric format specifiers. 如果 format 等于 String.Empty 或为,则 null 当前对象的返回值的格式 SByte 为常规格式说明符 ( "G" ) 。If format is equal to String.Empty or is null, the return value of the current SByte object is formatted with the general format specifier ("G"). 如果 format 是其他任何值,则该方法将引发 FormatExceptionIf format is any other value, the method throws a FormatException.

.NET Framework 提供了广泛的格式设置支持,以下格式设置主题更详细地介绍了这些支持:The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

provider参数是一个 IFormatProvider 实现。The provider parameter is an IFormatProvider implementation. GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关此方法返回的字符串格式的区域性特定信息。Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of the string returned by this method. ToString(String, IFormatProvider)调用方法时,它调用 provider 参数的 IFormatProvider.GetFormat 方法并向其传递一个 Type 表示类型的对象 NumberFormatInfoWhen the ToString(String, IFormatProvider) method is invoked, it calls the provider parameter's IFormatProvider.GetFormat method and passes it a Type object that represents the NumberFormatInfo type. GetFormat然后,方法返回 NumberFormatInfo 对象,该对象提供用于设置参数格式的信息 value ,如负号符号、组分隔符符号或小数点符号。The GetFormat method then returns the NumberFormatInfo object that provides information for formatting the value parameter, such as the negative sign symbol, the group separator symbol, or the decimal point symbol. 使用 provider 参数为方法提供格式设置信息的方法有三种 ToString(String, IFormatProvider)There are three ways to use the provider parameter to supply formatting information to the ToString(String, IFormatProvider) method:

  • 可以传递一个 CultureInfo 对象,该对象表示提供格式设置信息的区域性。You can pass a CultureInfo object that represents the culture that supplies formatting information. GetFormat 方法返回 NumberFormatInfo 对象,该对象提供该区域性的数字格式设置信息。Its GetFormat method returns the NumberFormatInfo object that provides numeric formatting information for that culture.

  • 可以传递 NumberFormatInfo 提供数字格式设置信息的实际对象。You can pass the actual NumberFormatInfo object that provides numeric formatting information. (其实现 GetFormat 仅返回自身。 ) (Its implementation of GetFormat just returns itself.)

  • 可以传递实现的自定义对象 IFormatProviderYou can pass a custom object that implements IFormatProvider. 它的 GetFormat 方法实例化并返回 NumberFormatInfo 提供格式设置信息的对象。Its GetFormat method instantiates and returns the NumberFormatInfo object that provides formatting information.

如果 providernull ,则返回的字符串的格式基于 NumberFormatInfo 当前区域性的对象。If provider is null, the formatting of the returned string is based on the NumberFormatInfo object of the current culture.

另请参阅

适用于

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 standard or custom numeric format string.

返回

String

此实例的值的字符串表示形式,由 format 指定。The string representation of the value of this instance as specified by format.

例外

format 无效。format is invalid.

示例

下面的示例初始化值的数组 SByte ,并通过使用每个标准格式字符串和一些自定义格式字符串来显示这些值。The following example initializes an array of SByte values and displays them by using each standard format string and some custom format strings.

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
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 使用当前区域性的约定来设置指定格式的值的格式。The ToString(String) method formats an SByte 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)

format参数可以是任何有效的标准数值格式说明符,也可以是自定义数字格式说明符的任意组合。The format parameter can be any valid standard numeric format specifier, or any combination of custom numeric format specifiers. 如果 format 等于 String.Empty 或为,则 null 当前对象的返回值的格式 SByte 为常规格式说明符 ( "G" ) 。If format is equal to String.Empty or is null, the return value of the current SByte object is formatted with the general format specifier ("G"). 如果 format 是其他任何值,则该方法将引发 FormatExceptionIf format is any other value, the method throws a FormatException.

.NET Framework 提供了广泛的格式设置支持,以下格式设置主题更详细地介绍了这些支持:The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

返回字符串的格式由 NumberFormatInfo 当前区域性的对象确定。The format of the returned string is determined by the NumberFormatInfo object for the current culture. 根据 format 参数,此对象控制输出字符串中的符号,如负号、组分隔符和小数点符号。Depending on the format parameter, this object controls symbols such as the negative sign, the group separator, and the decimal point symbol in the output string. 若要为当前区域性以外的区域性提供格式设置信息,请调用 ToString(String, IFormatProvider) 重载。To provide formatting information for cultures other than the current culture, call the ToString(String, IFormatProvider) overload.

另请参阅

适用于

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

此实例的值的字符串表示形式,由减号(如果值为负)和没有前导零的从 0 到 9 的数字序列组成。The string representation of the value of this instance, consisting of a negative sign if the value is negative, and a sequence of digits ranging from 0 to 9 with no leading zeroes.

示例

下面的示例 SByte 使用默认方法显示一个值 ToString()The following example displays an SByte value using the default ToString() method. 它还显示 SByte 使用多个标准格式说明符生成的值的字符串表示形式。It also displays the string representations of the SByte value that results from using a number of standard format specifiers. 这些示例使用 en-us 区域性的格式设置约定来显示。The examples are displayed using the formatting conventions of the en-US culture.

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
   }
}
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" 或常规) 格式的值的格式。The ToString() method formats an SByte value in the default ("G", or general) format of the current culture. 如果要指定其他格式或区域性,请使用方法的其他重载 ToString ,如下所示:If 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)

返回值使用一般数值格式说明符进行格式设置 ( "G" ) 值的字符串表示形式在 SByte 其值为负时包含负号,而从0到9之间的数字序列的位数不带前导零。The return value is formatted using the general numeric format specifier ("G") The string representation of the SByte value includes a negative sign if its value is negative, and a sequence of digits ranging from 0 to 9 without leading zeros. 负号由 NumberFormatInfo 当前区域性的对象定义。The negative sign is defined by the NumberFormatInfo object for the current culture.

若要定义带符号字节值的字符串表示形式的格式设置,请调用 ToString(String) 方法。To define the formatting of the signed byte value's string representation, call the ToString(String) method.

另请参阅

适用于