C# | Double.ToString() Method | Set – 1

Double.ToString() Method is used to convert the numeric value of the current instance to its equivalent string representation. There are 4 methods in the overload list of this method as follows:

  • ToString(String) Method
  • ToString() Method
  • ToString(IFormatProvider) Method
  • ToString(String, IFormatProvider) Method

Here, we will discuss the first two methods.

ToString(String) Method

This method is used to convert the numeric value of the current instance to its equivalent string representation, using the specified format.

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

Return Value: This method returns the string representation of the value of the current instance as specified by format.

Exceptions: This method throws FormatException if format is invalid.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

String value is 1.6326E+004

Example 2: For FormatException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Format is invalid.
Exception Thrown: System.FormatException

ToString() Method

This method is used to convert the numeric value of the current instance to its equivalent string representation.

Syntax: public override string ToString ();

Return Value: This method returns a string which represents the value of this instance.

Below programs illustrate the use of Double.ToString() Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

String value is 7.92281625142643E+21

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

String value is 20.3495
String value is 30.5945994
String value is 40
String value is 4294967295.5858

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.