Convert double to String in Java

Tags: String double Convert DecimalFormat

In this Java core tutorial, we learn how to convert double value into String value in Java via different solutions.

Using String.valueOf() method to convert a double value to String

ConvertDoubleToStringExample1.java

public class ConvertDoubleToStringExample1 {
    public static void main(String[] args) {
        double value1 = 12.0099;
        String value2 = String.valueOf(value1);

        System.out.println("double value: " + value1);
        System.out.println("String value: " + value2);
    }
}
The output is:
double value: 12.0099
String value: 12.0099

Using Double.toString() static method to convert a double value to String

ConvertDoubleToStringExample2.java

public class ConvertDoubleToStringExample2 {
    public static void main(String[] args) {
        double value1 = 12.4567;
        String value2 = Double.toString(value1);

        System.out.println("double value: " + value1);
        System.out.println("String value: " + value2);
    }
}
The output is:
double value: 12.4567
String value: 12.4567

Using Double.toString() method to return a String value from a Double object

ConvertDoubleToStringExample3.java

public class ConvertDoubleToStringExample3 {
    public static void main(String[] args) {
        Double value1 = 79.0009;
        String value2 = value1.toString();

        System.out.println("double value: " + value1);
        System.out.println("String value: " + value2);
    }
}
The output is:
double value: 79.0009
String value: 79.0009

Format double value as String using String.format() method

ConvertDoubleToStringExample4.java

public class ConvertDoubleToStringExample4 {
    public static void main(String[] args) {
        double value1 = 56.99898;
        String value2 = String.format("%.5f", value1);

        System.out.println("double value: " + value1);
        System.out.println("String value: " + value2);
    }
}
The output is:
double value: 56.99898
String value: 56.99898

Format double value as String using DecimalFormat class

ConvertDoubleToStringExample5.java

import java.text.DecimalFormat;

public class ConvertDoubleToStringExample5 {
    public static void main(String[] args) {
        double value1 = 15.090909;
        DecimalFormat decimalFormat = new DecimalFormat("#.######");
        String value2 = decimalFormat.format(value1);

        System.out.println("double value: " + value1);
        System.out.println("String value: " + value2);
    }
}
The output is:
double value: 15.090909
String value: 15.090909

Happy Coding 😊