beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java – Convert double to string example

By Chaitanya Singh | Filed Under: Java Conversion

In this java tutorial, we will learn how to convert double to string in Java. There are several ways we can do this conversion –
1. Java – Convert double to string using String.valueOf(double) method.
2. Convert double to string in Java using toString() method of Double wrapper class.
3. Java – double to string conversion using String.format() method
4. Convert double to string using DecimalFormat.format()
5. Java Convert double to string using StringBuffer and StringBuilder.

1. Java – Convert double to string using String.valueOf(double) method

public static String valueOf(double d): We can convert the double primitive to String by calling valueOf() method of String class. This method returns the string representation of the double argument.

public class JavaExample{  
   public static void main(String args[]){ 
	//double value
	double dnum = 99.9999;  
		
	//convert double to string using valueOf() method
	String str = String.valueOf(dnum);  
		
	//displaying output string after conversion
	System.out.println("My String is: "+str);  
   }
}

Output:
Java Convert double to String

2. Convert double to string in Java using toString() method of Double wrapper class

public String toString( ): This is the another method that can be used to convert double to String. This method returns a string representation of the Double object. The primitive double value represented by this object is converted to a string.

public class JavaExample{  
   public static void main(String args[]){ 
	double dnum = -105.556;  
		
	//double to string conversion using toString()
	String str = Double.toString(dnum);  
		
	System.out.println("My String is: "+str);
   }
}

Output:
double to string in Java using Double.toString()

3. Java – double to string conversion using String.format() method

String.format() method can be used for the double to string conversion.

public class JavaExample{  
   public static void main(String args[]){ 
	double dnum = -99.999;  
		
	String str = String.format("%f", dnum); 
		
	System.out.println("My String is: "+str);
   }
}

Output:
Java Convert double to string using String.format()

We can adjust the number of decimal digits in our string using this method. For example: If we want only two digits after decimal point in our string then we can change the code like this:

double dnum = -99.999;  
String str = String.format("%.2f", dnum);

The output of this code would be: My String is: -100.00

This is because this method round of the double value.

4. Convert double to string using DecimalFormat.format()

Similar to String.format() method. To use this, we have to import the package: java.text.DecimalFormat in our code.

import java.text.DecimalFormat;
public class JavaExample{  
   public static void main(String args[]){ 
	double dnum = -99.999;  
		
	/* creating instance of DecimalFormat
	 * #.000 is to have 3 digits after decimal point 
	 * in our output string
	 */
	DecimalFormat df = new DecimalFormat("#.000");
		
	//conversion
	String str = df.format(dnum);
		
	//displaying output
	System.out.println("My String is: "+str);
   }
}

Output:
Convert double to string in java using DecimalFormat

5. Java Convert double to string using StringBuffer and StringBuilder

We can convert double to string using StringBuffer and StringBuilder as well. The steps of conversion are same for both. The steps are as follows –
1. Create StringBuffer/StringBuilder instance
2. Append double value
3. Convert StringBuffer/StringBuilder to String
double ->StringBuffer ->String

public class JavaExample{  
   public static void main(String args[]){ 
	//double value
	double dnum = 89.891;
		
	//creating instance of StringBuffer
	StringBuffer sb = new StringBuffer();
		
	//appending the double value to StringBuffer instance
	sb.append(dnum);
		
	//converting StringBuffer to String
	String str = sb.toString();
		
	System.out.println("My String is: "+str);
   }
}

Output:

My String is: 89.891

double ->StringBuilder ->String

public class JavaExample{  
   public static void main(String args[]){ 
	//double value
	double dnum = -66.89;
		
	//creating instance of StringBuilder
	StringBuilder sb = new StringBuilder();
		
	//appending the double value to StringBuilder instance
	sb.append(dnum);
		
	//converting StringBuilder to String
	String str = sb.toString();
		
	System.out.println("My String is: "+str);
   }
}

Output:
Java Convert double to String using StringBuffer and StringBuilder

❮ PreviousNext ❯

Comments

  1. Claus says

    November 15, 2016 at 6:21 AM

    Here is another one:
    Double var = 33.33;
    String str = “” + var;
    Or is there any drawback using this?
    And it works with every proimitive data type.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Conversion

  • Java String to int
  • Java int to String
  • Java String to long
  • Java long to String
  • Java String to double
  • Java double to String
  • Java double to int
  • Java int to double
  • Java int to long
  • Java long to int
  • Java char to String
  • Java char to int
  • Java int to char
  • Java float to String
  • Java boolean to String
  • Java String to boolean
  • Java binary to decimal
  • Java decimal to binary
  • Java binary to Octal
  • Java decimal to hexadecimal
  • Java Hex to decimal
  • Java decimal to Octal
  • Java Octal to decimal
  • Java ASCII to String
  • Java Writer to String
  • Java StackTrace to String
  • Java String to ArrayList
  • Java StringBuffer to String
  • Java InputStream to String

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap