The primary goal to convert double to string is to store big streams of numbers that are coming where even data types fail to store the stream of numbers. It is generically carried out when we want to display the bigger values. Here it’s a double-in-text field GUI application because it becomes very easier for us to play with Java operations as everything is done in string format.
It becomes very crucial from the programmer’s perceptive as we will see often primitive datatypes being converted to string type as Java is object-oriented focusing on bringing it closer to real life because of which these conversions become very important.
Example of Converting Double to String
Input: 22.098 // Double Data
Output: 22.098 // String DataInput: 123.456 // Double Data
Output: 123.456 // String Data
Different Ways for Converting Double to String
There are different kinds of methods to convert double data to string data. Two standard approaches are as follows:
- Using valueOf() method of the String class
- Using toString() method of Double class
1. Using valueOf() Method of String Class
The valueOf() method of the String class is a method in Java that simply typecasts below-given parameters to strings always as it is an inbuilt method of the String class in Java. Do remember always any execution after making one execution using this method may change the result as it will concatenate leading to unguided output.
Syntax
String valueOf(double num);
- Parameters: It can hold objects, float, char, double, int, float, long, and character arrays.
- Return type: String
Java Program to Convert Double to String Using valueOf() Method
Java
// Java Program to Convert Double Data to a String Data // Using valueOf() Method of String Class // Importing Libraries import java.io.*; import java.util.*; // Driver Class class GFG { // Main driver function public static void main(String[] args) { // Declaring and initializing double number double number = 123.456 ; // Converting Double data to String data String output = String.valueOf(number); // Printing the above string System.out.println(output); } } |
123.456
To know more about the method refer to this article.
2. Using the toString() Method of Double Class
In Java whenever a print is called, always toString() method of Object Class in Java is always called be it directly or indirectly. Even if this inbuilt function is not used and the double number is getting printed then too toString() method is called. The string is a class in Java. All the classes in Java are derived from Object Classes. Whenever print is accessed where an object in a Java program is not created. Whenever the user gives a print command in Java, the toString() method of the Object class in Java is always called.
So, now if the user has created an object of his own type then there is an urgency to override the existing toString() method so that when the print command is called overridden toString() method is called. If no object is created by the user then it is known it is already an inbuilt method present already in the directory of java which will convert the double data to the string value.
Java Program to Convert Double to String Using toString() Method
Java
// Java Program to Convert Double Data to a String Data // Using toString() method of Double Class // Importing Libraries import java.io.*; import java.util.*; // Driver Class class GFG { // Main driver method public static void main(String[] args) { // Declaring and initializing number double number = 123.456 ; // Converting Double data to String data String output = Double.toString(number); // Printing above string on console System.out.println(output); } } |
123.456