Int64.ToString Int64.ToString Int64.ToString Int64.ToString Method

定义

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

重载

ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider)

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

ToString(String, IFormatProvider) ToString(String, IFormatProvider) ToString(String, IFormatProvider) 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) ToString(String) ToString(String) ToString(String)

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

ToString() ToString() ToString() ToString()

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

ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider)

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

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

参数

provider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

一个 IFormatProvider,它提供区域性特定的格式设置信息。An IFormatProvider that supplies culture-specific formatting information.

返回

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

实现

示例

下面的示例使用Int64 CultureInfo表示多个不同区域性的对象显示值的字符串表示形式。The following example displays the string representation of an Int64 value using CultureInfo objects that represent several different cultures.

long value = -16325901;
// 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:
//       -16325901
//       -16325901
//       -16325901
Dim value As Long = -16325801
' 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:
'       -16325901
'       -16325901
'       -16325901

注解

方法使用指定区域性Int64NumberFormatInfo对象设置默认值("G" 或 "常规")格式的值的格式。 ToString(IFormatProvider)The ToString(IFormatProvider) method formats an Int64 value in the default ("G", or general) format by using the NumberFormatInfo object of a specified culture. 如果要指定其他格式或当前区域性, 请使用ToString方法的其他重载, 如下所示:If 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 object that implements the IFormatProvider interface. GetFormat方法返回一个NumberFormatInfo对象,该对象提供有关此方法返回的字符串格式的区域性特定信息。Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of the string that is returned by this method. 实现IFormatProvider的对象可以是以下任一项:The object that implements IFormatProvider can be any of the following:

如果providernull或者无法从provider获取NumberFormatInfo对象,则使用当前区域性的对象格式化返回值。 NumberFormatInfoIf provider is null or a NumberFormatInfo object cannot be obtained from provider, the return value is formatted with the NumberFormatInfo object for the current culture.

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

另请参阅

ToString(String, IFormatProvider) ToString(String, IFormatProvider) ToString(String, IFormatProvider) 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);
override this.ToString : string * IFormatProvider -> string
Public Function ToString (format As String, provider As IFormatProvider) As String

参数

format
String String String String

一个数值格式字符串。A numeric format string.

provider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

一个对象,它提供有关此实例的区域性特定格式设置信息。An object that supplies culture-specific formatting information about this instance.

返回

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

实现

异常

format 无效或不受支持。format is invalid or not supported.

示例

下面的示例使用三个不同区域性的每个受支持的标准数值格式说明符显示正值和负值。The following example displays a positive and a negative value using each of the supported standard numeric format specifiers for three different cultures.

// Define cultures whose formatting conventions are to be used.
CultureInfo[] cultures = {CultureInfo.CreateSpecificCulture("en-US"), 
                          CultureInfo.CreateSpecificCulture("fr-FR"), 
                          CultureInfo.CreateSpecificCulture("es-ES") };
long positiveNumber = 1679;
long negativeNumber = -3045;
string[] specifiers = {"G", "C", "D8", "E2", "F", "N", "P", "X8"}; 

foreach (string specifier in specifiers)
{
   foreach (CultureInfo culture in cultures)
   {
      // Display values with "G" format specifier.
      Console.WriteLine("{0} 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 to the console:
//    G format using en-US culture:             1679            -3045
//    G format using fr-FR culture:             1679            -3045
//    G format using es-ES culture:             1679            -3045
//    
//    C format using en-US culture:        $1,679.00      ($3,045.00)
//    C format using fr-FR culture:       1 679,00 ?      -3 045,00 ?
//    C format using es-ES culture:       1.679,00 ?      -3.045,00 ?
//    
//    D8 format using en-US culture:         00001679        -00003045
//    D8 format using fr-FR culture:         00001679        -00003045
//    D8 format using es-ES culture:         00001679        -00003045
//    
//    E2 format using en-US culture:        1.68E+003       -3.05E+003
//    E2 format using fr-FR culture:        1,68E+003       -3,05E+003
//    E2 format using es-ES culture:        1,68E+003       -3,05E+003
//    
//    F format using en-US culture:          1679.00         -3045.00
//    F format using fr-FR culture:          1679,00         -3045,00
//    F format using es-ES culture:          1679,00         -3045,00
//    
//    N format using en-US culture:         1,679.00        -3,045.00
//    N format using fr-FR culture:         1 679,00        -3 045,00
//    N format using es-ES culture:         1.679,00        -3.045,00
//    
//    P format using en-US culture:     167,900.00 %    -304,500.00 %
//    P format using fr-FR culture:     167 900,00 %    -304 500,00 %
//    P format using es-ES culture:     167.900,00 %    -304.500,00 %
//    
//    X8 format using en-US culture:         0000068F         FFFFF41B
//    X8 format using fr-FR culture:         0000068F         FFFFF41B
//    X8 format using es-ES culture:         0000068F         FFFFF41B
' 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 Long = 1679
Dim negativeNumber As Long = -3045
Dim specifiers() As String = {"G", "C", "D8", "E2", "F", "N", "P", "X8"} 

For Each specifier As String In specifiers
   For Each culture As CultureInfo In Cultures
      ' Display values with "G" format specifier.
      Console.WriteLine("{0} format using {1} culture: {2, 16} {3, 16}", _ 
                        specifier, culture.Name, _
                        positiveNumber.ToString(specifier, culture), _
                        negativeNumber.ToString(specifier, culture))

   Next
   Console.WriteLine()
Next
' The example displays the following output to the console:
'    G format using en-US culture:             1679            -3045
'    G format using fr-FR culture:             1679            -3045
'    G format using es-ES culture:             1679            -3045
'    
'    C format using en-US culture:        $1,679.00      ($3,045.00)
'    C format using fr-FR culture:       1 679,00 ?      -3 045,00 ?
'    C format using es-ES culture:       1.679,00 ?      -3.045,00 ?
'    
'    D8 format using en-US culture:         00001679        -00003045
'    D8 format using fr-FR culture:         00001679        -00003045
'    D8 format using es-ES culture:         00001679        -00003045
'    
'    E2 format using en-US culture:        1.68E+003       -3.05E+003
'    E2 format using fr-FR culture:        1,68E+003       -3,05E+003
'    E2 format using es-ES culture:        1,68E+003       -3,05E+003
'    
'    F format using en-US culture:          1679.00         -3045.00
'    F format using fr-FR culture:          1679,00         -3045,00
'    F format using es-ES culture:          1679,00         -3045,00
'    
'    N format using en-US culture:         1,679.00        -3,045.00
'    N format using fr-FR culture:         1 679,00        -3 045,00
'    N format using es-ES culture:         1.679,00        -3.045,00
'    
'    P format using en-US culture:     167,900.00 %    -304,500.00 %
'    P format using fr-FR culture:     167 900,00 %    -304 500,00 %
'    P format using es-ES culture:     167.900,00 %    -304.500,00 %
'    
'    X8 format using en-US culture:         0000068F         FFFFF41B
'    X8 format using fr-FR culture:         0000068F         FFFFF41B
'    X8 format using es-ES culture:         0000068F         FFFFF41B

注解

方法使用指定的Int64区域性的NumberFormatInfo对象设置指定格式的值的格式。 ToString(String, IFormatProvider)The ToString(String, IFormatProvider) method formats an Int64 value in a specified format by using the NumberFormatInfo object 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 either a standard or a custom numeric format string. 除了 "R" (或 "r")以外的所有标准数字格式字符串,都支持所有自定义数字格式字符。All standard numeric format strings other than "R" (or "r") are supported, as are all custom numeric format characters. 如果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").

provider参数是IFormatProvider实现接口的对象。The provider parameter is an object that implements the IFormatProvider interface. GetFormat方法返回一个NumberFormatInfo对象,该对象提供有关此方法返回的字符串格式的区域性特定信息。Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of the string that is returned by this method. 实现IFormatProvider的对象可以是以下任一项:The object that implements IFormatProvider can be any of the following:

如果providernull或者无法从provider获取NumberFormatInfo对象,则此实例的返回值将使用当前区域性的进行格式化。 NumberFormatInfoIf provider is null or a NumberFormatInfo object cannot be obtained from provider, the return value for this instance is formatted with the NumberFormatInfo for the current culture.

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

另请参阅

ToString(String) ToString(String) ToString(String) 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);
override this.ToString : string -> string
Public Function ToString (format As String) As String

参数

format
String String String String

一个数值格式字符串。A numeric format string.

返回

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

异常

format 无效或不受支持。format is invalid or not supported.

示例

下面的示例使用每Int64个受支持的标准数值格式说明符以及两个自定义数字格式字符串来显示值。The following example displays an Int64 value using each of the supported standard numeric format specifiers together with two custom numeric format strings. 将数值转换为字符串时,此示例使用 en-us 区域性的格式设置约定。In converting the numeric values to strings, the example uses the formatting conventions of the en-US culture.

long value = -16325;
string specifier;

// Use standard numeric format specifier.
specifier = "G";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    G: -16325
specifier = "C";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    C: ($16,325.00)
specifier = "D8";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    D8: -00016325
specifier = "E4";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    E4: -1.6325E+004
specifier = "e3";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    e3: -1.633e+004
specifier = "F";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    F: -16325.00
specifier = "N";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    N: -16,325.00
specifier = "P";
Console.WriteLine("{0}: {1}", specifier, (value/100000.0).ToString(specifier));
// Displays:    P: -16.33 %
specifier = "X";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    X: FFFFFFFFFFFFC03B 

// Use custom numeric format specifiers.
specifier = "0,0.000";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    0,0.000: -16,325.000
specifier = "#,#.00#;(#,#.00#)";
Console.WriteLine("{0}: {1}", specifier, (value*-1).ToString(specifier));
// Displays:    #,#.00#;(#,#.00#): 16,325.00
Dim value As Long = -16325
Dim specifier As String

' Use standard numeric format specifier.
specifier = "G"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    G: -16325
specifier = "C"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    C: ($16,325.00)
specifier = "D8"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    D8: -00016325
specifier = "E4"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    E4: -1.6325E+004
specifier = "e3"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    e3: -1.633e+004
specifier = "F"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    F: -16325.00
specifier = "N"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    N: -16,325.00
specifier = "P"
Console.WriteLine("{0}: {1}", specifier, (value/100000).ToString(specifier))
' Displays:    P: -16.33 %
specifier = "X"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    X: FFFFFFFFFFFFC03B

' Use custom numeric format specifiers.
specifier = "0,0.000"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    0,0.000: -16,325.000
specifier = "#,#.00#;(#,#.00#)"
Console.WriteLine("{0}: {1}", specifier, (value*-1).ToString(specifier))
' Displays:    #,#.00#;(#,#.00#): 16,325.00

注解

方法使用表示当前Int64区域性约定的NumberFormatInfo对象来设置指定格式的值的格式。 ToString(String)The ToString(String) method formats an Int64 value in a specified format by using a NumberFormatInfo object that represents the conventions of the current culture. 如果要使用默认格式("G" 或 "常规")或指定不同的区域性,请使用ToString方法的其他重载,如下所示:If 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参数可以是任何有效的标准数值格式说明符("R" 除外)以及自定义数字格式说明符的任意组合。The format parameter can be any valid standard numeric format specifier except for "R", as well as any combination of custom numeric format specifiers. 如果formatnull或空字符串(""),则此实例的返回值使用一般数值格式说明符("G")进行格式设置。If format is null or an empty string (""), the return value of this instance is formatted with the general numeric format specifier ("G").

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

此实例的返回值的格式为当前区域性NumberFormatInfo的。The return value of this instance is formatted with the NumberFormatInfo for the current culture.

另请参阅

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

返回

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

示例

下面的示例使用默认Int64 ToString()方法显示一个值。The following example displays an Int64 value using the default ToString() method. 它还显示使用多个标准格式Int64说明符生成的值的字符串表示形式。It also displays the string representations of the Int64 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.

long value = -16325091;
// Display value using default ToString method.
Console.WriteLine(value.ToString());            // Displays -16325091
// Display value using some standard format specifiers.
Console.WriteLine(value.ToString("G"));         // Displays -16325091
Console.WriteLine(value.ToString("C"));         // Displays ($16,325,091.00)
Console.WriteLine(value.ToString("D"));         // Displays -16325091
Console.WriteLine(value.ToString("F"));         // Displays -16325091.00
Console.WriteLine(value.ToString("N"));         // Displays -16,325,091.00
Console.WriteLine(value.ToString("X"));         // Displays FFFFFFFFFF06E61D      
Dim value As Long = -16325091
' Display value using default ToString method.
Console.WriteLine(value.ToString())            ' Displays -16325091
' Display value using some standard format specifiers.
Console.WriteLine(value.ToString("G"))         ' Displays -16325091
Console.WriteLine(value.ToString("C"))         ' Displays ($16,325,091.00)
Console.WriteLine(value.ToString("D"))         ' Displays -16325091
Console.WriteLine(value.ToString("F"))         ' Displays -16325091.00
Console.WriteLine(value.ToString("N"))         ' Displays -16,325091.00
Console.WriteLine(value.ToString("X"))         ' Displays FFFFFFFFFF06E61D      

注解

方法使用当前区域性Int64NumberFormatInfo对象设置默认值("G" 或 "常规")格式的值的格式。 ToString()The ToString() method formats an Int64 value in the default ("G", or general) format by using the NumberFormatInfo object 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)

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

另请参阅

适用于