Java Convert double to String With Examples

Today we will see different ways to convert double to string in java. Java double to string conversion can be done in many ways, we will go through them one by one with example code snippets.

Java Convert Double to String

Let’s look at different code snippets for java double to string conversion. Note that double is a primitive data type whereas Double is an Object. However java supports autoboxing, so they both can be used interchangeably in most of the cases.

  1. Using + operator

    This is the easiest way to convert double to string in java.

  2. Double.toString()

    We can use Double class toString method to get the string representation of double in decimal points. Below code snippet shows you how to use it to convert double to string in java.

  3. String.valueOf()

  4. new Double(double l)

    Double constructor with double argument has been deprecated in Java 9, but you should know it.

  5. String.format()

    We can use Java String format method to convert double to String in our programs.

  6. DecimalFormat

    We can use DecimalFormat class to convert double to String. We can also get string representation with specified decimal places and rounding of half-up.

  7. StringBuilder, StringBuffer

    We can use StringBuilder and StringBuffer append function to convert double to string.

Java Double to String Example

Here is a simple program where we will convert double to string and print it using all the different methods we saw above.

That’s all for converting double to string in java program.

Reference: Double API Doc

By admin

Leave a Reply

%d bloggers like this: