User Brian
Brian
Level 41

Java Convert Double to String

Published in the Java Developer group

What is a “double”?

The primitive data type “double” in Java is used to store decimal numbers with a higher level of precision of upto 15 digits. While “float” provides 6-7 digits of precision, “double” is more widely used for higher precision of calculations and measurements.Java Convert Double to String - 1

What is a “Double” in Java?

“Double” is a wrapper class in Java that stores the primitive type of “double”. It comes with a bunch of useful functions for “double” manipulation and computations. For example, you can convert Java double to string quite effectively using its wrapper object.

How to convert Java Double to String?

You can convert double to string in Java by various simple methods. Let’s look at a few examples.

Double.toString() method


package com.doubletostring.java;
public class ConvertDoubleToString {

	public static void main(String[] args) {
	
		Double pi = 3.141592653589793;
		System.out.println("Pi Double: " + pi);
		
		// Converting Double to String using Double.toString() method
		String piStr = Double.toString(pi);
		System.out.println("Pi String: " + piStr);
		
	}
}

Output

Pi Double: 3.141592653589793 Pi String: 3.141592653589793
This is the most handy and commonly used approach for converting Java double(s) to string(s).

String.valueOf() method


package com.doubletostring.java;
public class StringValueOfDouble {

	public static void main(String[] args) {
		
		Double screwGaugeReading = 7.271572353580126;
		System.out.println("Screw Gauge Reading Double: " + screwGaugeReading);
		
		// Converting Double to String using String.valueOf() method
		String screwGaugeReadingStr = String.valueOf(screwGaugeReading);
		System.out.println("Screw Gauge Reading String: " + screwGaugeReadingStr);
	}
}

Output

Screw Gauge Reading Double: 7.271572353580126 Screw Gauge Reading String: 7.271572353580126

String.format() method


package com.doubletostring.java;
public class StringFormat {

	public static void main(String[] args) {

		Double vernierCalliper = 7.271572353580126;
		System.out.println("Vernier Calliper Double: " + vernierCalliper);
		
		// Converting Double to String using String.format() method
		// Format Literal "%s" returns String for any parameter data type 
		// Parameter is "Double" in our case
		String vernierCalliperStr = String.format("%s", vernierCalliper);
		System.out.println("Vernier Calliper String: " + vernierCalliperStr);
	}
}

Output

Vernier Calliper Double: 7.271572353580126 Vernier Calliper String: 7.271572353580126

Conclusion

By now, you must be familiar with different ways of converting the Java double to string. But in order to master anything, practise is the key. Good luck and happy coding!Java Convert Double to String - 2
Comments (1)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Oswal Kumar Level 0
17 July 2021
From your blog "Java Convert Double to String" I learned a lot of new things about it, and if anyone wants to do