Java Convert double to String

0Shares

Today in this article we will see how to convert double to string.in java we have many methods to convert double to string. we will go through all methods one by one with an easy example code snippets. if you did not install java on your device. for install Windows OS  or Mac OS-X

Java Convert double to String

Java Convert Double to String

So Friends lets we see some easy example in all code snippets. that cover all methods of convert double to string in java.we have total 7 methods. The most important thing is in java is double is a primitive data type and DOUBLE is Object.

1)Using + Operator

  • the best and easy method to convert double to string in java.
double x=789456.12d;
String z = x + ""; //convert string is 789456.12

2)Double.toStirng()

  • in this method, we use Double class toString() method. toString() is convert a string representation to decimal points.
double x=789456.12d;
String z= Double.toString(x);
System.out.println(z); //prints '789456.12'

3)String.valueOf()

  • this method returns the string representation of the double argument
double x=789456.12d;
String z= String.valueOf(x); // str is '789456.12'

4)new Double(double l)

  • Using Double Class parameterized constructor is now deprecated in after new version of JAVA 9.but as java developer you should be known about this.
double x = 78.94d;
String z = String.format("%f", d);
System.out.println(z); //78.94

5)StringBuilder, StringBuffer

  • String Builder and StringBuffer are converting double to string. using the append() function you can convert.
double x=789456.12d;
String str = new StringBuilder().append(x).toString();

6)DecimalFormat.format()

  • most look like to String.format() method
double x= -55.555;  
		
/* creating instance of DecimalFormat #.000 is to have 3 digits after decimal point  in our output string*/

DecimalFormat z= new DecimalFormat("#.000");
		
//conversion
String str = z.format(x);
		
//displaying output
System.out.println("My String is: "+str);

output is :
My String is: -55.555

7)String.format()

  • now last method is String.format() to convert double to string.
double x=78.94d;
String z = String.format("%f", x);
System.out.println(z); //78.940000

Java Double to String Example

In this example, we cover all the above method and try to understand throughout the easy example.we implement all 7 method

package com.androidcrown.double;

import java.text.DecimalFormat;

public class JavaDoubleToString {

public static void main(String[] args) {

   double x = 789456.12d;
   String z= Double.toString(x);
   System.out.println(z);

   z= String.valueOf(x );
   System.out.println(z);

   // deprecated from Java 9, use valueOf for better performance
   z= new Double(x.toString();
   System.out.println(z);

   z= String.format("%f", x);
   System.out.println(z); //789456.120000

   z= x + "";
   System.out.println(z);

   z= DecimalFormat.getNumberInstance().format(x);
   System.out.println(z);

   z= new DecimalFormat("#.0#").format(x);
   System.out.println(z);

   z= new StringBuilder().append(d).toString();
   System.out.println(z);
  }

}

Reference: Double API Doc by oracle

Conclusion:

In this article, we learned how to double data type to string type conversion in java using different kinds of methods with an easy example.

If you need any help or have any queries regarding it then leave a comment.

0Shares

Leave a Comment