
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java methods to convert Double to String
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
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
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
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
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
- Related Articles
- How to Convert Double to String to Double in Java?
- Convert double to string in Java
- Convert String to Double in Java
- Java Program to convert String to Double
- Convert double value to string in Java
- How to convert a double value to String in Java?
- Java Program to convert Double into String using toString() method of Double class
- Convert a String to a double type number in Java
- How to convert a Double array to a String array in java?
- Golang Program to convert string variables to double
- Convert string (varchar) to double in MySQL
- Convert Double to Integer in Java
- Golang Program to convert double type variables to string
- Swift program to convert double type variable to string
- Convert double primitive type to a Double object in Java
