此方法用於使用指定的區域性特定格式信息將數字的指定字符串表示形式轉換為等效的單精度浮點數。
用法:
public static float ToSingle (string value, IFormatProvider provider);
參數:
- value:它是一個包含要轉換的數字的字符串。
- provider:它是提供區域性特定格式信息的對象。
返回值:此方法返回一個單精度浮點數,該數字等於value中的數字;如果value為null,則返回0(零)。
異常:
- FormatException:如果該值不包含一個可選的符號,後跟一個數字序列(0到9)。
- OverFlowException:如果該值表示一個小於MinValue或大於MaxValue的數字。
以下示例程序旨在說明Convert.ToSingle(String,IFormatProvider)方法的用法:
範例1:
// C# program to demonstrate the
// Convert.ToSingle() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures = new CultureInfo("en-US");
// declaring and intializing String array
string[] values = {"123.45", "+12.345",
"-1234.5"};
// calling get() Method
Console.Write("Converted float value"
+ " from a specified sttring ");
for (int j = 0; j < values.Length; j++)
{
get(values[j], cultures);
}
}
catch (FormatException e)
{
Console.WriteLine("\n");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (OverflowException e)
{
Console.WriteLine("\n");
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string s,
CultureInfo cultures)
{
// converting string to specified float
float val = Convert.ToSingle(s, cultures);
// display the converted float value
Console.Write(" {0}, ", val);
}
}
輸出:
Converted float value from a specified sttring 123.45, 12.345, -1234.5,
範例2:對於FormatException
// C# program to demonstrate the
// Convert.ToSingle() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures = new CultureInfo("en-US");
// declaring and intializing String array
string[] values = {"12.345", "+1234.5",
"-123.45"};
// calling get() Method
Console.Write("Converted float value"
+ " of specified strings:");
for (int j = 0; j < values.Length; j++)
{
get(values[j], cultures);
}
Console.WriteLine("\n");
string s = "123 456, 789";
Console.WriteLine("format of s is invalid ");
// converting string to specified float
float val = Convert.ToSingle(s, cultures);
// display the converted float value
Console.Write(" {0}, ", val);
}
catch (FormatException e)
{
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (OverflowException e)
{
Console.Write("Exception Thrown:");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string s,
CultureInfo cultures)
{
// converting string to
// specified float value
float val = Convert.ToSingle(s, cultures);
// display the converted
// float value
Console.Write(" {0}, ", val);
}
}
輸出:
Converted float value of specified strings: 12.345, 1234.5, -123.45, format of s is invalid Exception Thrown:System.FormatException
參考:
相關用法
- C# Boolean.ToString(IFormatProvider)用法及代碼示例
- C# Convert.ToSByte(String, IFormatProvider)用法及代碼示例
- C# Convert.ToDecimal(String, IFormatProvider)用法及代碼示例
- C# Convert.ToInt32(String, IFormatProvider)用法及代碼示例
- C# Convert.ToString(String, IFormatProvider)用法及代碼示例
- C# Convert.ToInt64(String, IFormatProvider)用法及代碼示例
- C# Convert.ToChar(String, IFormatProvider)用法及代碼示例
- C# Convert.ToDouble(String, IFormatProvider)用法及代碼示例
- C# Convert.ToInt16(String, IFormatProvider)用法及代碼示例
- C# Convert.ToBoolean(String, IFormatProvider)用法及代碼示例
- C# Convert.ToUInt64(String, IFormatProvider)用法及代碼示例
- C# Convert.ToByte(String, IFormatProvider)用法及代碼示例
- C# Convert.ToDateTime(String, IFormatProvider)用法及代碼示例
- C# Convert.ToUInt16(String, IFormatProvider)用法及代碼示例
- C# Convert.ToUInt32(String, IFormatProvider)用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Convert.ToSingle(String, IFormatProvider) Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。