Double.ToString()方法用於將當前實例的數值轉換為其等效的字符串表示形式。此方法的重載列表中有4種方法,如下所示:
- ToString(String)方法
- ToString()方法
- ToString(IFormatProvider)方法
- ToString(String,IFormatProvider)方法
在這裏,我們將討論最後兩種方法。
ToString(IFormatProvider) Method
此方法用於使用指定的區域性特定格式信息將當前實例的數值轉換為其等效的字符串表示形式。
用法:
public string ToString (IFormatProvider provider);
參數:此方法采用IFormatProvider類型的對象,該對象提供特定於區域性的格式設置信息。
返回值:此方法以provider參數指定的格式返回當前實例的值的字符串表示形式。
例:
// C# program to demonstrate
// Double.ToString(IFormatProvider)
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// declaring and initializing Double value
double d1 = 568912.4587d;
// creating and initializing
// the object of CultureInfo
CultureInfo provider = new CultureInfo("en-us");
// using the method
string value = d1.ToString(provider);
// Display the value
Console.WriteLine("The Value is {0} and provider is {1}",
value, provider.Name);
}
}
輸出:
The Value is 568912.4587 and provider is en-US
Double.ToString(String, IFormatProvider) Method
此方法用於使用指定的格式和區域性特定的格式設置信息將當前實例的數值轉換為等效的字符串表示形式。
用法:
public string ToString (string format, IFormatProvider provider);
參數:
-
format:它是數字格式的字符串。
provider:它是提供區域性特定格式信息的對象。
返回值:此方法以provider參數指定的格式返回當前實例的值的字符串表示形式。
例:
// C# program to demonstrate the
// Double.ToString(String, IFormatProvider)
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// declaring and intializing Double value
double d1 = 7876312.7823d;
// creating and initializing
// the object of CultureInfo
CultureInfo provider = new CultureInfo("fr-FR");
// declaring and intializing format
string format = "E04";
// using the method
string val = d1.ToString(format, provider);
// Displaying the details
Console.WriteLine("The value is {0}",val);
Console.WriteLine("The Format is {0}",format);
Console.WriteLine("The Provider is {0}", provider.Name);
}
}
輸出:
The value is 7,8763E+006 The Format is E04 The Provider is fr-FR
參考:
相關用法
- C# Math.Max()用法及代碼示例
- C# Decimal.Add()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# SortedDictionary.Add()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 C# | Double.ToString() Method | Set – 2。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。