📜  带有示例的C#中的UInt64.ToString()方法|套装– 1

📅  最后修改于: 2021-05-29 20:36:47             🧑  作者: Mango

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

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

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

ToString(IFormatProvider)方法

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

句法:

public string ToString (IFormatProvider provider);

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

返回值:该方法返回当前实例的值的字符串表示形式,该值由介于0到9之间的数字序列组成,没有符号或前导零。

例子:

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

UInt64.ToString(String,IFormatProvider)方法

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

句法:

public string ToString (string format, IFormatProvider provider);

参数:

返回值:该方法返回由formatprovider参数指定的当前实例的字符串表示形式

异常:如果format参数无效,则此方法将给出FormatException

例子:

// C# program to demonstrate the
// UInt64.ToString(String, 
// IFormatProvider) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // declaring and intializing UInt64 value
        ulong s1 = UInt64.MaxValue;
  
        // 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 = s1.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 18446744073709551615
The Format is D5
The Provider is fr-FR

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.uint64.tostring?view=netstandard-2.1