當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。