Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java Program to Convert Double to String

  • Last Updated : 26 May, 2021

Problem Statement: Java program to convert double datatype to strings in java using inbuilt functions. The primary goal for this is to store big streams of numbers that are coming where even data types fail to store the stream of numbers. 

Illustration:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Input: 22.098 // Double Data
Output: 22.098 // String Data

Input: 123.456 // Double Data
Output: 123.456 // String Data

There are different kinds of methods to convert double data to string data. Two standard approaches are as follows:

Approaches:



Approach 1: Using String.valueOf Method

String.valueOf() is a method in java which simply typecasts below-given parameter to strings always as it is an inbuilt method os 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.

Example:

int a = 10, int b = 20;
String s1 = String.valueOf(a);
String s2 = String.valueOf(b);
System.out.println(s1+s2);      || Output: 30       || Addition of a and b via s1+s2
System.out.println(s1+s2);      || Output: 1020     || Concatenation of s1 & s2

Parameters it can hold:

object, float, char, double, int, float, long, char[]

Return type:

Always String

This method is an inbuilt method present already in the directory of java which returns the string object as discussed. Here is an example of implementation in java: 

Java




// Java program to convert double data to a string data
 
// Importing Libraries
import java.util.*;
import java.io.*;
 
class GFG {
 
    // Main driver function
    public static void main(String[] args)
    {
        // Declaring the number
        double number = 123.456;
 
        // Converting Double data to String data
        String output = String.valueOf(number);
 
        // Printing the string data
        System.out.println(output);
    }
}

 
 

Output:



 

123.456

Approach 2: Using Double.toString() method

 

Double.toString(): 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 Class. Whenever print is accessed where object in a java program is not created. Whenever the user gives a print command in java, toString() method of 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 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.

 

 The use of this method is depicted in the below example:

 

Java




// Java program to convert double data to a string data
 
// Importing Libraries
import java.util.*;
import java.io.*;
 
class GFG {
 
    // Main driver function
    public static void main(String[] args)
    {
        // Declaring the number
        double number = 123.456;
 
        // Converting Double data to String data
        String output = Double.toString(number);
 
        // Printing the string data
        System.out.println(output);
    }
}

 
 

Output:

 

123.456

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :