Java Program to Convert Double to String (original) (raw)

Last Updated : 25 Dec, 2023

The primary goal of double to String conversion in Java 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. In this article, we will learn How to Convert double to String in Java.

Program to Convert double to String in Java

Below is the implementation of double to String conversion using ‘+’ :

Java

import java.io.*;

class GFG {

`` public static void main(String[] args)

`` {

`` double num = 12.345 ;

`` String str = num + "" ;

`` System.out.println(str);

`` System.out.println(

`` "Type of num : "

`` + ((Object)num).getClass().getSimpleName());

`` System.out.println(

`` "Type of str : "

`` + ((Object)str).getClass().getSimpleName());

`` }

}

Output

12.345 Type of num : Double Type of str : String

Explaination of the above Program:

In above program we are converting num(Double) to String using ‘+’ Operator.

Different Methods for Converting double to String in Java

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

Method Class Description Syntax
valueOf() method String The valueOf() method simply typecasts below-given parameters to strings always as it is an inbuilt method of the String class in Java. String **valueOf(double num);
format() method String format() Method belongs to String and it uses precision modifier to convert double to string. String.format(“%f”,variable_name)
append() Method from StringBuilder String append() Method from StringBuilder Class is used to append the element to the end of the StringBuilder Object. String str= new StringBuilder().append(variable_name).toString();
toString() method Decimal 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. Double.toString(var_name);
DecimalFormat Decimal DecimalFormat is a feature in Java in which we can format the decimal DecimalFormat.getNumberInstance().format(var_name);

**Example of Methods to Convert double to String in Java

Below is the implementation of Methods to Convert double to String:

Java

import java.io.*;

import java.text.DecimalFormat;

import java.util.*;

class GFG {

`` public static void main(String[] args)

`` {

`` double number = 123.456 ;

`` String method1 = String.valueOf(number);

`` System.out.println( "Using valueOf method "

`` + method1);

`` String method2 = String.format( "%f" , number);

`` System.out.println( "Using format method "

`` + method2);

`` String method3

`` = new StringBuilder().append(number).toString();

`` System.out.println( "Using append method "

`` + method3);

`` String method4 = Double.toString(number);

`` System.out.println( "Using toString method "

`` + method4);

`` String method5

`` = DecimalFormat.getNumberInstance().format(

`` number);

`` System.out.println( "Using Decimalformat method "

`` + method5);

`` }

}

Output

Using valueOf method 123.456 Using format method 123.456000 Using append method 123.456 Using toString method 123.456 Using Decimalformat method 123.456

Similar Reads

Java Basic Programs





















Java Pattern Programs














Java Conversion Programs















Java Classes and Object Programs
















Java Methods Programs











Java Searching Programs




Java 1-D Array Programs













Java 2-D Arrays (Matrix) Programs













Java String Programs