当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Double.ToString()方法用法及代码示例


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

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

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

ToString(String) Method

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



用法: public string ToString (string format);
Here, it takes a numeric format string.

返回值:此方法返回格式所指定的当前实例的值的字符串表示形式。

异常:如果格式无效,则此方法引发FormatException。

范例1:

// C# program to demonstrate the 
// Double.ToString(String) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // Declaring and initializing value 
            double value = 16325.62d; 
  
            // 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 1.6326E+004

范例2:对于FormatException

// C# program to demonstrate the 
// Double.ToString(String) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // Declaring and initializing value 
            double value = 16325.62d; 
  
            // 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() Method

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

用法: public override string ToString ();

返回值:此方法返回一个字符串,代表该实例的值。



下面的程序说明Double.ToString()方法的使用:

范例1:

// C# program to demonstrate the 
// Double.ToString()  Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // Declaring and initializing value 
        double value = 7922816251426433759354.39503305d; 
  
        // using ToString() method 
        string round = value.ToString(); 
  
        // Display the value 
        Console.WriteLine("String value is {0}", round); 
    } 
}
输出:
String value is 7.92281625142643E+21

范例2:

// C# program to demonstrate the 
// Double.ToString() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // calling get() method 
        get(20.3495d); 
        get(30.5945994d); 
        get(40d); 
        get(4294967295.5858d); 
    } 
  
    // defining get() method 
    public static void get(double value) 
    { 
  
        // using ToString() method 
        string round = value.ToString(); 
  
        // Display the value 
        Console.WriteLine("String value is {0}", round); 
    } 
}
输出:
String value is 20.3495
String value is 30.5945994
String value is 40
String value is 4294967295.5858

参考:




相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Double.ToString() Method | Set – 1。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。