dineshonjava

Convert Double to String to Double in Java with Example

Convert Double to String to Double in Java
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.