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 `

// Java Program to Convert // Double to String Using '+' import java.io.*;

// Driver Class class GFG { // main function public static void main(String[] args) { // Double declared double num = 12.345;

    // Converting Double to String
    String str = num + "";

    System.out.println(str);

    // Type of num
    System.out.println(
        "Type of num : "
        + ((Object)num).getClass().getSimpleName());

    // Converted element to string
    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 `

// Java Program to Convert // Double Data to a String Data

// Importing Libraries import java.io.; import java.text.DecimalFormat; 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 method1 = String.valueOf(number);
    System.out.println("Using valueOf method "
                       + method1);

    // Conversion using format()
    String method2 = String.format("%f", number);
    System.out.println("Using format method "
                       + method2);

    // Conversion using append()
    String method3
        = new StringBuilder().append(number).toString();
    System.out.println("Using append method "
                       + method3);

    // Converting Double data to String data
    String method4 = Double.toString(number);
    System.out.println("Using toString method "
                       + method4);

    // Converting using DecimalFormat
    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