• Register
3,890 points
11 5 2

Double.ToString() method is used for converting double values to its equivalent string representation. There are four methods available to perform this functionality;

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

These methods are used to convert the number of values of current variables to its equivalent string representation by using the specified formats.

Syntax:

public string ToString (string format);

// Here this function will take numeric format string.z

Return Value:

This method will return the string representation of a specified value by using this method.

Example 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);

                               }

               }

}

Output:

String Value is 1.6326E+004

Example 2 :

public class DoubleToStringExample2{  

public static void main(String args[]){ 

double d=89.7; 

String s=Double.toString(d);

System.out.println(s);

}}

Output:

89.7

 

3,890 points
11 5 2