Kamis, 13 Maret 2014

How to Convert Double to String to Double in Java Program with Example






Many scenarios come 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 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. These conversion example assumes code is running above Java 5 version and actually tested in JDK 1.6, which makes it enable to pass Double object when 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





convert double to String to double in Java


There are at least three way to convert a String, representing double value, into a Double Object. There could be more ways to do the same , Please let us know if you know any other method of conversion which is not listed here.





1) First way of converting a String to Double is just create a new Double object. Double has a constructor which expects a String value and it 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) Second way of String to double conversion is by using parseDouble(String str) from Double class. by far this is my preferred method because its more readable and standard way of converting a string value to double. here is an example :





Double doubleString = Double.parseDouble(toBeDouble);





Again you need to take care of NumberFormatException which can be occur while converting an invalid double string to double object.








3) Third way to change String into Double is by using Double.valueOf(String str) method. Just pass your double string into this method and it will 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 we will see atleast four ways of doing same. this is rather much easier than opposite.





1) First way to convert Double to string is using concatantion operator "+" which produce a new string. This is by far simplest way of converting double object to string.





Double toBeString = 400.40;


String fromDouble = "" + toBeString;








2) Second way of double to String conversion is by using String.valueOf(double d) method , which takes a double value as argument and returns 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) Third way to convert double into String is by using toString() method of Double Class, which is essentially the same way used in first way because concatanation oeprator internally calls toString() method of object to get its string value.





String stringDouble = toBeString.toString();







4) Fourth way is rather more 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 an String upto two decimal point or three decimal point based on your requirement.








 String convertedString = String.format("%.3f", toBeString);


 


This convertedString contains double value upto 3 decimal point. "f" is used to format floating point numbers. As you may have noticed we are passing Double object to methods which are expecting double premitive value and that is only possible due to autoboxing. if you are running below Java 5 use intValue() doubleValue() methods to get value in primitive format.








These are some basic ways to change any String into Double wrapper Class and vice versa. Please let us know if you are familiar with any other utility or method or doing same thing with less hassle may be like overriding equals using EqualsBuilder and HashCodeBuilder.








Some of my other Java articles:
































Source:http://javarevisited.blogspot.com/2011/10/convert-double-to-string-example.html

Tidak ada komentar:

Posting Komentar