📜  C#| Int64.ToString方法|套装– 1

📅  最后修改于: 2021-05-29 13:55:55             🧑  作者: Mango

Int64.ToString方法用于将当前实例的数值转换为其等效的字符串表示形式。此方法的重载列表中有4种方法,如下所示:

  1. ToString(IFormatProvider)方法
  2. ToString(String,IFormatProvider)方法
  3. ToString()方法
  4. ToString(String)方法

在这里,我们将讨论前两种方法。

ToString(IFormatProvider)方法

此方法用于使用指定的区域性特定格式信息将该实例的数值转换为其等效的字符串表示形式。

句法:

public string ToString (IFormatProvider provider);

参数:此方法采用IFormatProvider类型的对象,该对象提供特定于区域性的格式设置信息。

返回值:该方法以provider参数指定的格式返回当前对象的值的字符串表示形式。

例子:

// C# program to demonstrate
// Int64.ToString(IFormatProvider)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // declaring and initializing
        // Int64 value
        long val = Int64.MaxValue;
  
        // creating and initializing
        // the object of CultureInfo
        CultureInfo provider = new CultureInfo("en-us");
  
        // using the method
        string str = val.ToString(provider);
  
        // Display the value
        Console.WriteLine("The Value is {0} and provider is {1}",
                                             str, provider.Name);
    }
}
输出:
The Value is 9223372036854775807 and provider is en-US

Int64.ToString(String,IFormatProvider)方法

此方法用于使用指定的格式和特定于区域性的格式设置信息将此实例的数值转换为其等效的字符串表示形式。

例子:

// C# program to demonstrate the
// Int64.ToString(String, IFormatProvider)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // declaring and intializing
        // Int64 value
        long val = 4627836321;
  
        // creating and initializing
        // the object of CultureInfo
        CultureInfo provider = new CultureInfo("fr-FR");
  
        // declaring and intializing format
        string format = "D5";
  
        // using the method
        string str = val.ToString(format, provider);
  
        // Displaying the details
        Console.WriteLine("The value is {0}",str);
        Console.WriteLine("The Format is {0}",format);
        Console.WriteLine("The Provider is {0}", provider.Name);
    }
}
输出:
The value is 4627836321
The Format is D5
The Provider is fr-FR

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.Int64.tostring?view=netframework-4.7.2