Examples
- How to Convert Char to String in Java? Character to String
- How to Convert Char to Int in Java? Character to Integer Conversion
- How to Convert Int to Char in Java? Integer to Character
- How to Convert Long to Int in Java? Long to Integer
- How to Convert Int to Long in Java? Integer to Long Program
- How to Convert Boolean to String in Java? (Parse Bool to String)
- How to Convert String to Boolean in Java? 3 Ways With Program
- How to Convert String to Int in Java? String to Integer Program
- How to Convert Int to String in Java? Integer to String Program
- How to Convert Int to Double in Java? (Integer to Double)
- How to Convert Double to Int in Java? 4 Ways
- How to Convert String to Double in Java? Programs
- How to Convert Double to String in Java? Programs
How to Convert String to Double in Java? Programs
In programming, the ability to seamlessly manipulate data is paramount. One common task you'll encounter as a Java developer is the need to convert data from one type to another. One such transformation involves how to convert string to double in Java programming.
String is a sequence of characters, and double is a numerical representation. Whether you're building financial applications, scientific simulations, or just parsing user input, mastering the skills to convert string to double in Java is a fundamental skill.
Let's begin and understand the string to double conversion in Java language with programs and examples.
Concepts to Learn for This Program:
Java String to Double Conversion Using Double.parseDouble()
Here's a Java program to convert a string to a double using the Double.parseDouble() method.
Code
public class StringToDoubleExample {
public static void main(String[] args) {
// Step 1: Define a string containing a numerical value
String strNumber = "3.14159";
// Step 2: Use Double.parseDouble() to convert the string to a double
double doubleValue = Double.parseDouble(strNumber);
// Step 3: Print the result
System.out.println("String value: " + strNumber);
System.out.println("Double value: " + doubleValue);
}
}
Output
String value: 3.14159
Double value: 3.14159
Explanation
-
We start by defining a string variable strNumber with the value "3.14159", which represents a numerical value in string format.
-
We use the Double.parseDouble() method to convert the string strNumber to a double. This method takes the string as an argument and returns the equivalent double value.
-
Finally, we print the original string value and the converted double value to the console using System.out.println().
Convert String to Double in Java Using Double.valueOf()
Here's a Java program on how to convert a string to a double using the Double.valueOf() method.
Code
public class StringToDoubleExample {
public static void main(String[] args) {
// Step 1: Define a string containing a numerical value
String strNumber = "3.14159";
// Step 2: Use Double.valueOf() to convert the string to a Double object
Double doubleObject = Double.valueOf(strNumber);
// Step 3: Convert the Double object to a primitive double using the .doubleValue() method
double doubleValue = doubleObject.doubleValue();
// Step 4: Print the result
System.out.println("String value: " + strNumber);
System.out.println("Double value: " + doubleValue);
}
}
Output
String value: 3.14159
Double value: 3.14159
Explanation
-
We start by defining a string variable strNumber with the value "3.14159", which represents a numerical value in string format.
-
We use the Double.valueOf() method to convert the string strNumber to a Double object. This method takes the string as an argument and returns a Double object.
-
To obtain the primitive double value from the Double object, we use the .doubleValue() method.
-
Finally, we print the original string value and the converted double value to the console using System.out.println().
Convert String to Double in Java Using new Double(string)
Here's a program on how to convert a string to a double in Java using the new Double(string) method, although it's deprecated in modern Java.
Code
public class StringToDoubleExample {
public static void main(String[] args) {
// Step 1: Define a string containing a numerical value
String strNumber = "3.14159";
// Step 2: Use the deprecated constructor to convert the string to a Double object
Double doubleObject = new Double(strNumber);
// Step 3: Convert the Double object to a primitive double using the .doubleValue() method
double doubleValue = doubleObject.doubleValue();
// Step 4: Print the result
System.out.println("String value: " + strNumber);
System.out.println("Double value: " + doubleValue);
}
}
Output
String value: 3.14159
Double value: 3.14159
Explanation
-
We start by defining a string variable strNumber with the value "3.14159", which represents a numerical value in string format.
-
We use the deprecated constructor new Double(strNumber) to convert the string strNumber to a Double object. Note that this constructor is deprecated in modern Java versions.
-
To obtain the primitive double value from the Double object, we use the .doubleValue() method.
-
Finally, we print the original string value and the converted double value to the console using System.out.println().
Learn Next: