Java Convert Double to String Example

In this article, we will show you how to convert Java double to String code examples. Performing conversion from wrapper class Double or primitive type double to String is a common scenario when programming with Core Java.
In this article, we will show you 7 different ways we can do convert Java wrapper class Double or primitive type double to String.
  1. Convert using Double.toString(double)
  2. Convert using String.valueOf(double)
  3. Convert using new Double(double).toString()
  4. Convert using String.format()
  5. Convert using DecimalFormat
  6. Convert using StringBuffer
  7. Convert using StringBuilder

1. Convert using Double.toString(double)

Double.toString(double) - Returns a string representation of the double argument. All characters mentioned below are ASCII characters.
// Convert using Double.toString(int)
double numDouble = 100.25;
String doubleAsString = Double.toString(numDouble);
System.out.println("Convert using Double.toString(int) -> " + doubleAsString);
Output:
Convert using Double.toString(int) -> 100.25

2. Convert using String.valueOf(double)

String.valueOf(double) - Returns the string representation of the double argument. The representation is exactly the one returned by the Double.toString method of one argument.
// Convert using String.valueOf(int)
double numDouble = 100.25;
String doubleAsString1 = String.valueOf(numDouble);
System.out.println("Convert using String.valueOf(int) -> " + doubleAsString1);
Output:
Convert using String.valueOf(int) -> 100.25

3. Convert using new Double(double).toString()

Another alternative method is to create an instance of a Double class and then invoke it's toString() method.
// Convert using new Double(double).toString()
double numDouble = 100.25;
Double doubleInstance = new Double(numDouble);
String doubleAsString2 = doubleInstance.toString();
System.out.println("Convert using new Double(int).toString() -> " + doubleAsString2);
Output:
Convert using new Long(int).toString() -> 100.25

4. Convert using String.format()

String.format() is a new alternative that can be used for converting a Double to a String object.
// Convert using String.format()
double numDouble = 100.25;
String numberAsString3 = String.format ("%f", numDouble);
System.out.println("Convert using String.format() -> " + numberAsString3);
Output:
Convert using String.format() -> 100.250000

5. Convert using DecimalFormat

The class java.text.DecimalFormat is a class that formats a number to a String representation.
// Convert using DecimalFormat
double numDouble1 = 12345.10;
DecimalFormat decimalFormat = new DecimalFormat("#");
String numberAsString4 = decimalFormat.format(numDouble1);
System.out.println(doubleAsString);
System.out.println("Convert using DecimalFormat -> " + numberAsString4);
Output:
Convert using DecimalFormat -> 12345

6. Convert using StringBuffer

StringBuffer is a class that is used to concatenate multiple values into a String.
// Convert using StringBuffer Example
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(numDouble);
String numberAsString5 = stringBuffer.toString();
System.out.println("Convert using StringBuffer Example -> " + numberAsString5);
Output:
Convert using StringBuffer Example -> 100.25

7. Convert using StringBuilder

StringBuilder is a class that is used to concatenate multiple values into a String.
// Convert using StringBuilder Example
StringBuilder sbBuilder = new StringBuilder();
sbBuilder.append(numDouble);
String numberAsString6 = sbBuilder.toString();
System.out.println("Convert using StringBuilder Example  -> " + numberAsString6);
Output:
Convert using StringBuilder Example  -> 100.25

Complete Program for Reference

import java.text.DecimalFormat;

/**
 * ConvertDoubleToStringExample
 * @author javaguides.net
 *
 */
public class ConvertDoubleToStringExample {
 public static void main(String[] args) {
  
  // Convert using Double.toString(int)
  double numDouble = 100.25;
  String doubleAsString = Double.toString(numDouble);
  System.out.println("Convert using Double.toString(int) -> " + doubleAsString);

  // Convert using String.valueOf(int)
  String doubleAsString1 = String.valueOf(numDouble);
  System.out.println("Convert using String.valueOf(int) -> " + doubleAsString1);

  // Convert using new Double(int).toString()
  Double doubleInstance = new Double(numDouble);
  String doubleAsString2 = doubleInstance.toString();
  System.out.println("Convert using new Double(int).toString() -> " + doubleAsString2);

  // Convert using String.format()
  String numberAsString3 = String.format ("%f", numDouble);
  System.out.println("Convert using String.format() -> " + numberAsString3);

  // Convert using DecimalFormat
  double numDouble1 = 12345.10;
  DecimalFormat decimalFormat = new DecimalFormat("#");
  String numberAsString4 = decimalFormat.format(numDouble1);
  System.out.println(doubleAsString);
  System.out.println("Convert using DecimalFormat -> " + numberAsString4);

  // Convert using StringBuffer Example
  StringBuffer stringBuffer = new StringBuffer();
  stringBuffer.append(numDouble);
  String numberAsString5 = stringBuffer.toString();
  System.out.println("Convert using StringBuffer Example -> " + numberAsString5);

  // Convert using StringBuilder Example
  StringBuilder sbBuilder = new StringBuilder();
  sbBuilder.append(numDouble);
  String numberAsString6 = sbBuilder.toString();
  System.out.println("Convert using StringBuilder Example  -> " + numberAsString6);
 }
}
Output:
Convert using Double.toString(int) -> 100.25
Convert using String.valueOf(int) -> 100.25
Convert using new Double(int).toString() -> 100.25
Convert using String.format() -> 100.250000
100.25
Convert using DecimalFormat -> 12345
Convert using StringBuffer Example -> 100.25
Convert using StringBuilder Example  -> 100.25

Related Java String Conversion Examples

Comments