Java methods to convert Double to String

JavaObject Oriented ProgrammingProgramming

Following are methods available in Java using which you can convert Double value to String −

The toString() method of the class Double

This method returns the String format of the current Double object. To convert Double value to String.

  • Read the required primitive double value in to the Double class reference variable (autoboxing happens).

  • Convert it into a String using the toString() method.

Note − You can directly pass the double value to the toString() method directly −

Example

 Live Demo

import java.util.Scanner;
public class ConversionOfDouble {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a double value:");
      Double d = sc.nextDouble();
      String result = d.toString();
      System.out.println("The result is: "+result);
      System.out.println(Double.toString(d));
   }
}

Output

Enter a double value:
2548.2325
The result is: 2548.2325
2548.2325

The valueOf() method of the String class

This method accepts a char or, char array or, double or, float or, int or, long or an object as a parameter and returns its String representation. To convert Double value to String −

  • Get the double value.

  • Pass it as a parameter to this method and retrieve its String format.

Example

 Live Demo

import java.util.Scanner;
public class ConversionOfDouble {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a double value:");
      Double d = sc.nextDouble();
      String result = "".valueOf(d);
      System.out.println("The result is: "+result);
   }
}

Output

Enter a double value:
2548.2325
The result is: 2548.2325

The format() method of the String class

This method accepts a format String and arguments (varargs) and returns a String object of the given variable(s) in the specified format. To convert Double value to String −

  • Get the double value.

  • Invoke the format() method by passing “%f” as the format string along with the double value.

Example

 Live Demo

import java.util.Scanner;
public class ConversionOfDouble {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a double value:");
      double d = sc.nextDouble();
      String result = String.format("%f", d);
      System.out.println("The result is: "+result);
   }
}

Output

Enter a double value:
2548.2325
The result is: 2548.2325

The append() method of StringBuilder or StringBuffer

The append() method of the StringBuilder or StringBuffer objects accept a boolean or, char or, char array or, double or, float or, int or, long or, String value as parameter and adds it to the current object.

  • Get the double value.

  • Append it to the StringBuffer objet using the append() method.

  • Retrieve string value of the StringBuffer object using the toString() method.

Example

 Live Demo

import java.util.Scanner;
public class ConversionOfDouble {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a double value:");
      double d = sc.nextDouble();
      StringBuffer sb = new StringBuffer();
      sb.append(d);
      String result = sb.toString();
      System.out.println("The result is: "+result);
   }
}

Output

Enter a double value:
2548.2325
The result is: 2548.2325
raja
Published on 15-Oct-2019 07:33:34
Advertisements