Convert Double to String in Java

Introduction

Converting a double value to a string is a common task in Java. It can be useful in various scenarios, such as formatting numbers for display, logging, and manipulating numerical data. This blog post will explore different methods for converting a double to a string in Java.

Table of Contents

  1. Using Double.toString()
  2. Using String.valueOf()
  3. Using DecimalFormat
  4. Using String.format()
  5. Complete Example Program
  6. Conclusion

1. Using Double.toString()

The Double.toString() method is a static method in the Double class that converts a double value to its string representation.

Example:

public class DoubleToStringUsingToString {
    public static void main(String[] args) {
        double doubleValue = 123.456;

        // Convert double to string using Double.toString()
        String strValue = Double.toString(doubleValue);

        System.out.println("Double value: " + doubleValue);
        System.out.println("String value: " + strValue);
    }
}

Output:

Double value: 123.456
String value: 123.456

Explanation:

  • Double.toString(doubleValue) converts the double value to its string representation.

2. Using String.valueOf()

The String.valueOf() method is a static method in the String class that can convert various data types to their string representation, including double values.

Example:

public class DoubleToStringUsingValueOf {
    public static void main(String[] args) {
        double doubleValue = 789.123;

        // Convert double to string using String.valueOf()
        String strValue = String.valueOf(doubleValue);

        System.out.println("Double value: " + doubleValue);
        System.out.println("String value: " + strValue);
    }
}

Output:

Double value: 789.123
String value: 789.123

Explanation:

  • String.valueOf(doubleValue) converts the double value to its string representation.

3. Using DecimalFormat

The DecimalFormat class allows you to format decimal numbers to a specific pattern, which can then be converted to a string.

Example:

import java.text.DecimalFormat;

public class DoubleToStringUsingDecimalFormat {
    public static void main(String[] args) {
        double doubleValue = 456.789;

        // Convert double to string using DecimalFormat
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        String strValue = decimalFormat.format(doubleValue);

        System.out.println("Double value: " + doubleValue);
        System.out.println("String value: " + strValue);
    }
}

Output:

Double value: 456.789
String value: 456.79

Explanation:

  • DecimalFormat("#.##") creates a pattern to format the double value to two decimal places.
  • decimalFormat.format(doubleValue) converts the double value to its string representation with the specified pattern.

4. Using String.format()

The String.format() method allows you to format a string using format specifiers.

Example:

public class DoubleToStringUsingFormat {
    public static void main(String[] args) {
        double doubleValue = 987.654;

        // Convert double to string using String.format()
        String strValue = String.format("%.2f", doubleValue);

        System.out.println("Double value: " + doubleValue);
        System.out.println("String value: " + strValue);
    }
}

Output:

Double value: 987.654
String value: 987.65

Explanation:

  • String.format("%.2f", doubleValue) formats the double value to a string with two decimal places.

5. Complete Example Program

Here is a complete program that demonstrates all the methods discussed above to convert a double to a string.

Example Code:

import java.text.DecimalFormat;

public class DoubleToStringExample {
    public static void main(String[] args) {
        double doubleValue1 = 123.456;
        double doubleValue2 = 789.123;
        double doubleValue3 = 456.789;
        double doubleValue4 = 987.654;

        // Using Double.toString() Method
        String strValue1 = Double.toString(doubleValue1);
        System.out.println("Using Double.toString():");
        System.out.println("Double value: " + doubleValue1 + " -> String value: " + strValue1);

        // Using String.valueOf() Method
        String strValue2 = String.valueOf(doubleValue2);
        System.out.println("\nUsing String.valueOf():");
        System.out.println("Double value: " + doubleValue2 + " -> String value: " + strValue2);

        // Using DecimalFormat
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        String strValue3 = decimalFormat.format(doubleValue3);
        System.out.println("\nUsing DecimalFormat:");
        System.out.println("Double value: " + doubleValue3 + " -> String value: " + strValue3);

        // Using String.format() Method
        String strValue4 = String.format("%.2f", doubleValue4);
        System.out.println("\nUsing String.format():");
        System.out.println("Double value: " + doubleValue4 + " -> String value: " + strValue4);
    }
}

Output:

Using Double.toString():
Double value: 123.456 -> String value: 123.456

Using String.valueOf():
Double value: 789.123 -> String value: 789.123

Using DecimalFormat:
Double value: 456.789 -> String value: 456.79

Using String.format():
Double value: 987.654 -> String value: 987.65

6. Conclusion

Converting a double to a string in Java can be accomplished in several ways. The Double.toString() method and String.valueOf() method are both straightforward and widely used. Using DecimalFormat and String.format() provides additional control over the formatting of the double value. By understanding these different methods, you can choose the one that best fits your needs and coding style.

Happy coding!

Comments