In this core java tutorial we will learn how to convert String to double primitive type in java and convert String to double object with program and examples in java. We will also understand When to use convert String to double primitive type and when when to convert String to double object in java.
Programs to convert String to double in java -
- Program/Example 1 to How to convert String to double primitive type in java.
- Program/Example 2 to How to convert String to double object in java.
Program/Example 1 to How to convert String to double primitive type in java
package convertStringToDoublePkg;
public class ConvertStringToDoubleExample1 {
public static void main(String[] args) {
String string = "12";
try {
double d = Double.parseDouble(string);
System.out.println("double number = " + d);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
/*OUTPUT
double number = 12.0
*/
|
If in case invalid string is passed which can't get converted to double will throw NumberFormatException in java.
Program/Example 2 to How to convert String to double object in java
Double.valueOf(string) method returns double object.
package convertStringToDoublePkg;
public class ConvertStringToDoubleExample2 {
public static void main(String[] args) {
String string = "12";
Double doubleObject = Double.valueOf(string);
System.out.println("double doubleObject = " + doubleObject);
}
}
/*OUTPUT
double doubleObject = 12.0
*/
|
Programs to convert double to String in java -
- Program/Example 3 to How to convert double primitive type to String in java.
- Program/Example 4 to How to convert double object to String in java
Now, in this core java tutorial we will learn how to convert double primitive type to String in java and convert double object to String with program and examples in java. We will also understand When to use convert String to double primitive type and when when to convert String to double object in java.
Program/Example 3 to How to convert double primitive type to String in java.
String.valueOf(double) method converts double primitive type to String in java.
package convertDoubleToStringPkg;
public class ConvertDoubleToStringExample1 {
public static void main(String[] args) {
double d = 12d;
String string = String.valueOf(d);
System.out.println("string = "+string);
}
}
/*OUTPUT
string = 12.0
*/
|
Program/Example 4 to How to convert double object to String in java
toString() method of java.lang.Double class convert double object to String in java.
package convertDoubleToStringPkg;
public class ConvertDoubleObjectToStringExample2 {
public static void main(String[] args) {
Double doubleObject = 12d;
String string = doubleObject.toString();
System.out.println("string = "+string);
}
}
/*OUTPUT
string = 12.0
*/
|
Summary -
- In this core java tutorial we learned conversion of String to double primitive type in java and convert String to double object with program and examples in java.
- we also learned conversion of double primitive type to String in java and convert double object to String with program and examples in java.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy(JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>