Many scenarioscome in day to day Java programming when we need to convert a Double value to String or vice versa. In my earlier article we have seen how to convert String to Integer and in this article, we will first see how to convert double to String and the later opposite of that from String to double. One important thing to note is Autoboxing which automatically converts primitive type to Object type and only available from Java 5 onwards. This conversion example assumes the code is running above Java 5 version and actually tested in JDK 1.6, which makes it unable to pass Double object when the method is expecting double primitive value e.g. String.valueOf(double d) which expect a double value.
In case, you want to use these example of String to float prior to Java 5 you probably want to change a bit and use intValue(), doubleValue() method to convert primitive from Object type.
Converting String to Double in Java
There are at least three ways to convert a String, representing double value, into a Double Object. There could be more ways to do the same , Pleaselet us know if you know any other method of conversion which is not listed here.
1) The first way of converting a String to Double has just created a new Double object. Double has a constructor which expects a String value andit return a Double object with same value.
String toBeDouble = "200.20";
Double fromString = new Double(toBeDouble);
Beware of NumberFormatException which will occur if the String is not representing a valid Double value.
2) The second way of String to double conversion is by using parseDouble(String str) from Double class. by far this is my preferred method becauseit's more readable and standard way of converting a string value to double. here is an example :
Again you need to take care of NumberFormatException which can occur while converting an invalid double string to double object.
3) A third way to change String into Double is by using Double.valueOf(String str) method. Just pass your double string into this method and itwill convert to equivalent Double value.
Double doubleStr = Double.valueOf(toBeDouble);
This method can also throw NumberFormatException if String is null or not convertible to double. though Whitespace is ignored by Java.
Convert Double to String in Java Program
Converting Double to String in Java
As with above examples, there are multiple ways to convert a Double object into String. In this example of converting double to String, wewill see at least four ways of doing same. this is rather much easier than the opposite.
1) The first way to convert Double to string is using concatenation operator "+" which produce a new string. This is by far the simplest way ofconverting a double object to a string.
Double toBeString = 400.40;
String fromDouble = "" + toBeString;
2) The second way of double to String conversion is by using String.valueOf(double d) method , which takes a double value as an argument andreturns it in a form of String literal. here is an example of converting double to String using valueOf() method.
String strDouble = String.valueOf(toBeString);
3) A third way to convert double into String is by using toString() method of Double Class, which is essentially the same way used in a first way because concatenation operator internally calls toString() method of an object to get its string value.
String stringDouble = toBeString.toString();
4) The fourth 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.
This convertedString contains double value up to 3 decimal points. "f" is used to format floating point numbers.As you may have noticed we are passing the Double object to methods which are expecting double primitive value and that is only possibledue to autoboxing. if you are running below Java 5 use intValue() doubleValue() methods to get value in primitive format.