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.
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.
This is the easiest way to convert double to string in java.
double d = 123.45d;
String str = d+""; // str is '123.45'
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'
double d = 123.456d;
String str = String.valueOf(d); // str is '123.456'
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);
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
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'
We can use StringBuilder and StringBuffer append function to convert double to string.
double d = 123.45d;
String str = new StringBuilder().append(d).toString();
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.