Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Convert Double to String in Java

In this tutorial, we will learn how to convert primitive Double or Java wrapper double class object to String in Java.

Sometimes, we need to convert values from one data type to another, under certain situation.

For example, if we need to append a double value to a JTextArea, first we need to convert it to a string before appending to JTextArea.

Therefore, this tutorial will cover seven different ways to convert a primitive type double or Double wrapper class object to string in Java. Different ways for converting double to string are as:

  • Convert using Double.toString(double)
  • Convert using String.valueOf(double)
  • Converting using new Double(double).toString()
  • Convert using String.format()
  • Convert using DecimalFormat
  • Converting using StringBuffer
  • Convert using StringBuffer

Let’s understand all the above ways one by one with example programs.

Converting Double to String in Java using String.valueOf()


To convert primitive type double to string in Java, we use valueOf() method of the String class. The method String.valueOf() converts double to string. It is a static utility method of the String class.

Therefore, we do not need to create an object to calling this method. We can simply call by using its class name. The general signature of valueOf() method is as below:

public static String valueOf(double d)

This method accepts an integer value that needs to be converted and returns the string representation of the double value. Let’s create a simple program to convert a primitive double to string value in Java.

Program code 1:

// Java Program to demonstrate the conversion of double into string using String.valueOf().
package javaConversion;
public class DoubleToStringConversion {
public static void main(String[] args) 
{
 double num = 589.999;  
// Call valueOf() method of String class for converting double to string. 
   String str = String.valueOf(num);  
   System.out.println(num + 200); // 789.999 because + is binary plus operator .
   System.out.println(str + 200);// 789.999200 because + is string concatenation operator.
 }
}
Output:
      789.999
      589.999200

Converting Double to String in Java using Double.toString(double)


The Double wrapper class in Java also provides a toString() method that converts double numeric value to String. The toString() method of Double class is a static utility method. It returns a string object representing the specified double value.

The general signature of this method is as below:

public static String toString(double d)

Let’s create a simple Java program to convert a double value to string in Java using Double.toString() method.

Program code 2:

// Java Program to demonstrate the conversion of double into string using Double.toString().
package javaConversion;
public class DoubleToStringConversion {
public static void main(String[] args) 
{
 double num = 589.999;  
// Call toString() method of Double class for converting double to string. 
   String str = Double.toString(num); 
   System.out.println(num + 200); // 789.999 because + is binary plus operator .
   System.out.println(str + 200);// 789.999200 because + is string concatenation operator.
 }
}
Output:
      789.999
      589.999200

Converting Double to String using new Double(double).toString()


We can also convert double to string by creating Double class object and then invoking its toString() method. Look at the below example program based on it.

Program code 3:

// Java Program to demonstrate the conversion of double into string using new Double(double).toString().
package javaConversion;
public class DoubleToStringConversion {
public static void main(String[] args) 
{
 double num = 589.999;  
 String str = new Double(num).toString();  
 System.out.println("Numeric value: " +num);
 System.out.println("String representation: " +str);
 }
}
Output:
      Numeric value: 589.999
      String representation: 589.999

Convert Double to String using String.format()


The String.format() method of String class formats given arguments into String. Java has introduced it since JDK 1.5. This is a new alternative way for converting a Double class object to a string object.

The purpose of this method is to format a string. The general syntax for this method is as below:

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

Let’s create a Java program to convert a primitive double to string in Java using format() method of String class.

Program code 4:

// Java Program to demonstrate the conversion of double value into string using format() method.
package javaConversion;
public class DoubleToStringConversion {
public static void main(String[] args) 
{
 double num = 999.555;  
 String str = String.format("%f", num); 
 System.out.println("String representation: " +str);
 System.out.println(str + num);
 }
}
Output:
     String representation: 999.555000
     999.555000999.555

Converting Double to String using DecimalFormat


DecimalFormat is a concrete subclass of NumberFormat class that formats a number to a string representation. It provides a lot of features designed to parse and format numbers.

We can use it to format a number to a string representation using a certain pattern. Here’s an example program using DecimalFormat.

Program code 5:

// Java Program to demonstrate the conversion of double into string using DecimalFormat.
package javaConversion;
import java.text.DecimalFormat;
public class DoubleToStringConversion {
public static void main(String[] args) 
{
 double num = 999.555;  
 DecimalFormat numberFormat = new DecimalFormat("##,###");
 String str = numberFormat.format(num);        
 System.out.println("Number to be converted is: " + num);
 System.out.println("String representation is: " + str);
 }
}
Output:
      Number to be converted is: 999.555
      String representation is: 1,000

Convert Double to String in Java using StringBuffer


StringBuffer is a class that concatenates multiple values into a single string. It is thread-safe, but it is slower.

The StringBuffer object represents a String object that we can change and treat as an array with a sequence of characters.

To add a new value to the end of the string, StringBuffer instance implements the append() method. At the end, call the toString() method, in order to take the string representation of the data.

Here is an example program based on the conversion of double to String in Java.

Program code 6:

// Java Program to demonstrate the conversion of double into string using StringBuffer.
package javaConversion;
public class DoubleToStringConversion {
public static void main(String[] args) 
{
 double num = 777.555;  
// Create StringBuffer object.  
   StringBuffer sb = new StringBuffer();
   sb.append(num);
   String str = sb.toString();
   System.out.println("String representation is: " + str);
   System.out.println(str + num);
 }
}
Output:
      String representation is: 777.555
      777.555777.555

You can also write the following code to converting a primitive double type to string in Java.

String str = new StringBuffer().append(777.555).toString();
System.out.println("String representation: " + str);

Converting Double to String using StringBuilder


Let’s create a Java program to convert double numeric value to string representation using StringBuilder class. We can change (or modify) the content of StringBuilder, without creating a new StringBuilder object.

Program code 7:

// Java Program to demonstrate the conversion of double into string using StringBuilder.
package javaConversion;
public class DoubleToStringConversion {
public static void main(String[] args) 
{
 double num = 777.999;  
// Create StringBuilder object.  
   StringBuilder sbBuilder = new StringBuilder();
   sbBuilder.append(num);
   String str = sbBuilder.toString();
   System.out.println("String representation: " + str);
 }
}
Output:
      String representation: 777.999

In this tutorial, you learned how to convert a primitive type double to string in Java in seven ways. Hope that you will have understood and practiced all programs based on the conversion of double to string in Java.
Thanks for reading!!!

The post How to Convert Double to String in Java appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

How to Convert Double to String in Java

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×