📜  C#中的Single.ToString方法|套装– 1

📅  最后修改于: 2021-05-29 19:12:31             🧑  作者: Mango

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

  • ToString(String)方法
  • ToString()方法
  • ToString(IFormatProvider)方法
  • ToString(String,IFormatProvider)方法

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

ToString(String)方法

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

范例1:

// C# program to demonstrate the
// Single.ToString(String) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing value
            float value = 565998.1564f;
  
            // Declaring and initializing format
            string s = "E04";
  
            // using the method
            string str = value.ToString(s);
  
            // Display the value
            Console.WriteLine("String value is {0}", str);
        }
  
        catch (FormatException e) 
        {
            Console.WriteLine("Format is invalid.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
String value is 5.6600E+005

示例2:对于FormatException

// C# program to demonstrate the
// Single.ToString(String) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing value
            float value = 1623325.62412f;
  
            // Declaring and initializing format
            string s = "a";
  
            // using the method
            string str = value.ToString(s);
  
            // Display the value
            Console.WriteLine("String value is {0}", str);
        }
  
        catch (FormatException e)
        {
            Console.WriteLine("Format is invalid.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
Format is invalid.
Exception Thrown: System.FormatException

ToString()方法

此方法用于将当前实例的数值转换为其等效的字符串表示形式。

下面的程序说明了Single.ToString()方法的用法

范例1:

// C# program to demonstrate the
// Single.ToString() Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Declaring and initializing value
        float value = 792759354.39503305f;
  
        // using ToString() method
        string res = value.ToString();
  
        // Display the value
        Console.WriteLine("String value is {0}", res);
    }
}
输出:
String value is 7.927594E+08

范例2:

// C# program to demonstrate the
// Single.ToString() Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // calling get() method
        get(Single.MaxValue);
        get(Single.MinValue);
        get(40f);
        get(424967295.5858f);
    }
  
    // defining get() method
    public static void get(float value)
    {
  
        // using ToString() method
        string res = value.ToString();
  
        // Display the value
        Console.WriteLine("String value is {0}", res);
    }
}
输出:
String value is 3.402823E+38
String value is -3.402823E+38
String value is 40
String value is 4.249673E+08

参考:

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