Single.ToString()方法用於將當前實例的數值轉換為其等效的字符串表示形式。此方法的重載列表中有4種方法,如下所示:
- ToString(String)方法
- ToString()方法
- ToString(IFormatProvider)方法
- ToString(String,IFormatProvider)方法
在這裏,我們將討論最後兩種方法。
ToString(IFormatProvider) Method
此方法用於使用指定的區域性特定格式信息將當前實例的數值轉換為其等效的字符串表示形式。
用法:
public string ToString (IFormatProvider provider);
參數:此方法采用IFormatProvider類型的對象,該對象提供特定於區域性的格式設置信息。
返回值:此方法以provider參數指定的格式返回當前實例的值的字符串表示形式。
例:
// C# program to demonstrate
// Single.ToString(IFormatProvider)
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// declaring and initializing
// Single value
float s1 = 65465.67867f;
// 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 65465.68 and provider is en-US
Single.ToString(String, IFormatProvider) Method
此方法用於使用指定的格式和特定於區域性的格式設置信息將此實例的數值轉換為其等效的字符串表示形式。
用法:
public string ToString (string format, IFormatProvider provider);
參數:
-
format:它是標準或自定義數字格式的字符串。
provider:它是提供區域性特定格式信息的對象。
返回值:此方法返回由format和provider參數指定的當前實例的字符串表示形式。
例:
// C# program to demonstrate the
// Single.ToString(String, IFormatProvider)
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// declaring and intializing Single value
float s1 = Single.MaxValue;
// creating and initializing
// the object of CultureInfo
CultureInfo provider = new CultureInfo("es-ES");
// declaring and intializing format
string format = "N";
// 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 340.282.300.000.000.000.000.000.000.000.000.000.000,00 The Format is N The Provider is es-ES
參考:
相關用法
- C# Dictionary.Add()用法及代碼示例
- C# Math.Max()用法及代碼示例
- C# Math.Min()用法及代碼示例
- C# Decimal.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# SortedDictionary.Add()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Exp()用法及代碼示例
注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 Single.ToString() Method in C# | Set – 2。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。