Core JAVA

Convert Double to String to Double in Java with Example

In my earlier tutorial we have seen how to Convert Integer to String in Java. In this tutorial we will see how to convert Double to String and its vice versa. In Java application many times we need to convert a Double to String and its vice versa. From Java 5, Java was introduced new feature Auto-boxing which automatically converts primitive type to Object type. Means double automatically converted to Double object. So we can also convert String to double and double to String object.

Let’s see the conversions for String to Double and Double to String

Converting String to Double in Java

In java there are more ways to converting String to Double object or double value as per as our need. So let’s discuss some frequently used ways for this conversion.

1. If you want to convert String value to Double value then simply you could create the new object of Double class. Double class has number of constructors but it has one constructor which expects a String value and it return a Double object with same value.

For Example:

String doubleStr = "99.9";
Double doubleObj = new Double(doubleStr);

But doubleStr is not representing a valid number then it will occur NumberFormatException.

2. Java has another way to convert String object to Double object by using valueOf() static method of Double class. This method return Double object. You just pass your double string into this method and it will convert to equivalent Double value.

For Example:

String doubleStr = "99.9";
Double doubleObj = Double.valueOf(doubleStr);

This method can also throw NumberFormatException if String is null or not valid to double. Above two conversions always give us Double object but if want exact double value instead of Double object then try with following another way.

3. If you want to convert your string to double value as primitive type then you could this way. According this way of String to double conversion is by using parseDouble(String str) from Double class. And also this recommended way to converting String to double if want simple value because it’s more readable and standard way of converting a string value to double.

For Example:

String doubleStr = "99.9";
double doubleVal = Double.parseDouble(doubleStr);

This is static method of Double class and it will throw NumberFormatException if doubleStr is not representing a valid number.

Converting Double to String in Java

As above conversion we have seen String to Double, here we will see number ways to convert Double to String object. In the example we are converting double to String. Let’s see below points

1. It very easiest way to converting double value to String value by using concatenation with empty string which produce a new string.

For Example:

Double doubleObj = 99.9;
String doubleStr = "" + doubleObj;

2. String class has number of static methods. One of them is valueOf() method. We can also use this method for converting double to String. This method takes a double value as an argument and returns it in a form of String literal.

For Example:

Double doubleObj = 99.9;
String doubleStr = String.valueOf(doubleObj);

3. Double class has one of method toString(). Double class overrides this method of Object class. This is also one of very simple way to convert double into String by using toString() method of Double Class.

For Example:

Double doubleObj = 99.9;
String doubleStr = doubleObj.toString();

4. We can also use formatter of String class. In the String class there are lot of String.format() overloaded methods. We can use one of them for conversion of double to String. This way is rather more a flexible way of getting String from Double. it uses String.format() method and returns a formatted string so you can control the precision level and get a String up to two decimal points or three decimal points based on your requirement.

For Example:

Double doubleObj = 99.99999;
String doubleStr = String.format("%.2f", doubleObj);

This method return formatted string value which contains double value up to 2 decimal points. Here “f” is used to format floating point numbers.

Summary
We have seen different ways to converting String to Double and its vice versa. I always prefer parseDouble() method of Double class for converting String to double primitive type.

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago