Tutorial

Java Convert double to String

Published on August 3, 2022
Default avatar

By Pankaj

Java Convert double to String

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel