Report this

What is the reason for this report?

Java Convert double to String

Published on August 3, 2022
Java Convert double to String

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.

    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 our products

About the author

Pankaj Kumar
Pankaj Kumar
Author
See author profile

Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev

Category:
Tags:

Still looking for an answer?

Was this helpful?
Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.