Java Convert double to String

Filed Under: Java

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

java convert double to string, java 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.

    
    double d = 123.45d;
    String str = d+""; // str is '123.45'
    
  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.

    
    double d = 123.45d;
    String str = Double.toString(d);
    System.out.println(str); //prints '123.45'
    
  3. String.valueOf()

    
    double d = 123.456d;
    String str = String.valueOf(d); // str is '123.456'
  4. new Double(double l)

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

    
    double d = 123.45d;
    //deprecated from Java 9, use valueOf for better performance
    String str = new Double(d).toString();
    System.out.println(str);
    
  5. String.format()

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

    
    double d = 36.98d;
    String s = String.format("%f", d);
    System.out.println(s); //36.980000
    
  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.

    
    double d = 123.454d;
    String str = DecimalFormat.getNumberInstance().format(d);
    System.out.println(str); //str is '123.454'
    //if you don't want formatting
    str = new DecimalFormat("#.0#").format(d); // rounded to 2 decimal places
    System.out.println(str); //str is '123.45'
    str = new DecimalFormat("#.0#").format(123.456); // rounded to 2 decimal places
    System.out.println(str); //str is '123.46'
    
  7. StringBuilder, StringBuffer

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

    
    double d = 123.45d;
    String str = new StringBuilder().append(d).toString();
    

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.


package com.journaldev.string;

import java.text.DecimalFormat;

public class JavaDoubleToString {

	public static void main(String[] args) {
		double d = 123.45d;
		String str = Double.toString(d);
		System.out.println(str);

		str = String.valueOf(d);
		System.out.println(str);

		// deprecated from Java 9, use valueOf for better performance
		str = new Double(d).toString();
		System.out.println(str);

		str = String.format("%f", d);
		System.out.println(str); //123.450000

		str = d + "";
		System.out.println(str);

		str = DecimalFormat.getNumberInstance().format(d);
		System.out.println(str);

		str = new DecimalFormat("#.0#").format(d);
		System.out.println(str);

		str = new StringBuilder().append(d).toString();
		System.out.println(str);
	}

}

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

Reference: Double API Doc

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages