JavaDevNotes.com

Java Double To String Examples

This post will show examples on how to convert Java Double to String. It is a commonly encountered case to convert a double value to String when programming with the Java language.
Here are some examples for performing the conversion:

Convert using Double.toString(double)

The Double class has a static method that returns a String object representing the specified double parameter. This is a very efficient solution.

Syntax

public static String toString(double d)

This returns the string representation of the double argument. The argument d is converted to it's string representation and returns the String instance. If the number is negative, the sign will be preserved.

Example
double number = -895.25;
String numberAsString = Double.toString(number);
The code is equivalent to:
String numberAsString = "-895.25";
This is the most common and popular solution for converting a double to String.


Convert using String.valueOf(double)

The String class has several static methods that can convert most primitive types to their String representation. This includes doubles.

Example
double number = -895.25;
String numberAsString = String.valueOf(number);
or
String numberAsString = String.valueOf( -895.25);
This is also an efficient as the first solution ( Double.toString(double)) shown above. Because it is so simple, it is also popular solution for converting a double to String.


Convert using new Double(double).toString()

Another alternative method is to create an instance of Double class and then invoke it's toString() method. This is a little less efficient than the first two options shown above. This is because a new instance of Double is created before conversion is performed.

Example

double number = -895.25;
Double doubleInstance = new Double(number);      
String numberAsString = doubleInstance.toString();

We can shortened to:

double number = -895.25;
String numberAsString = new Double(number).toString();

or just:
String numberAsString = new Double(-895.25).toString();
Usually, this method should be used when the variable you wish to convert is already an instance of the Double class. If you only have a primitive variable (double), use the first two options mentioned above.


Convert using String.format()

String.format() is a new alternative that can be used for converting a Double to String object. It is first introduced in Java 5 (JDK1.5) and has many cool features.

Syntax
public static String format(String format, Object... args)

There are many options on how to use this method. But for conversion purposes, here is a simple example:

Example
double number = -895.25f;
String numberAsString = String.format ("%f", number);

And the variable numberAsString will have the value "-895.250000"

We can also control the number of decimal places.

double number = -895.25f;
System.out.println(String.format ("%.1f", number));
System.out.println(String.format ("%.2f", number));
System.out.println(String.format ("%.3f", number));
Will display
-895.3
-895.25
-895.250

If you are using Java 5 or higher, this is also a very simple alternative for converting a double to String object.



Convert using DecimalFormat

The class java.text.DecimalFormat is a class that formats a number to a String representation following a certain pattern.
Example

double number = -100895.256;
DecimalFormat decimalFormat = new DecimalFormat("#.00");
String numberAsString = decimalFormat.format(number);
System.out.println(numberAsString);
Will output
-100895.26

This is an example that will convert to String but with commas
double number = -100895.256;
DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
String numberAsString = decimalFormat.format(number);
System.out.println(numberAsString);
Will output
-100,895.26
This is ideal if you are looking for a solution where you have more control on the resulting String. You can specify comma separator for readability and control the number of decimal places.


Convert using StringBuffer or StringBuilder

StringBuffer is a class that is used to concatenate multiple values into a String. StringBuilder works similarly but not thread safe like StringBuffer. These two classes can be used to convert a Java Double to String.

StringBuffer Example

double number = -895.25;
StringBuffer sb = new StringBuffer();
sb.append(number);
String numberAsString = sb.toString();

StringBuilder Example

double number = -895.25;
StringBuilder sb = new StringBuilder();
sb.append(number);
String numberAsString = sb.toString();

Shortened Examples

String numberAsString = new StringBuffer().append(-895.25).toString();

String numberAsString = new StringBuilder().append(-895.25).toString();


Quick Solution

If you want something simple that works but not elegant, you can use this code:
double number = -895.25;
String numberAsString = "" + number;

When you concatenate a String and a Double, the result is a String. Internally, the code is inefficient because there are intermediate objects that are created behind the scene.



String Tutorials And Examples