📌  相关文章
📜  C#| Convert.ToSingle(String,IFormatProvider)方法

📅  最后修改于: 2021-05-29 18:56:23             🧑  作者: Mango

此方法用于使用指定的区域性特定格式信息将数字的指定字符串表示形式转换为等效的单精度浮点数。

句法:

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

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.convert.tosingle?view=netframework-4.7.2#System_Convert_ToSingle_System_String_System_IFormatProvider_