BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java – Convert double to string example

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

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 ❯

Top Related Articles:

  1. Java StringBuilder offsetByCodePoints()
  2. Java StringBuffer class With Examples
  3. how to copy one hashmap content to another hashmap
  4. Java – float to String conversion
  5. Java Math.nextAfter() Method

Tags: Java-Conversion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

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 Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap