
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Double Value to String in Java
The double data type in Java stores double-precision 64-bit IEEE 754 floating point number. It is used as the default type for decimal value in Java.
Like all other primitive variables double also have a wrapper class (Double) wrapping the primitive data type. Since java supports auto boxing, the primitive value and the object can be used interchangeably.
You can convert a double value into String in various different ways in Java −
Using the “+” operator −
The + operator is an addition operator but when used with Strings it acts as a concatenation operator. It concatenates the other operand to String and returns a String object.
You can convert a double value into a String by simply adding it to an empty String using the “+” operator. In-fact it is the easy way to convert a double value to a String.
Example
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; System.out.println("The result is: "+result); } }
Output
Enter a double value: 2548.2325 The result is: 2548.2325
Using the toString() method of the Double class −
The toString() method of the wrapper class Double returns the String format of the current Double object.
Read the required primitive double value in to the Double class reference variable (auto-boxing happens) and, convert it into a String using the toString() method.
Example
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); } }
Output
Enter a double value: 2548.2325 The result is: 2548.2325
Or, you can directly pass the double value to the toString() method directly −
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 = Double.toString(d); System.out.println("The result is: "+result); } }
Using the valueOf() method of the String class −
The valueOf() method of the String class 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.
You can pass the required double value as a parameter to this method and retrieve its Sting format.
Example
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
Using the format() method of the String class −
This method accepts a format String and arguments (var-args) and returns a String object of the given variable(s) in the specified format.
You can format a double value into a String using the format() method. To it pass “%f” as the format string (along with the required double value).
Example
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
Using 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.
You can append the required double value to the method and retrieve a String from obtained StringBuffer (or, StringBuilder) objects.
Example
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