Convert string to double in Java
In this post, you will learn how to convert string to double in the Java programming language.
In the Java programming language, strings are objects that represent a sequence of characters. Java strings are created and manipulated through the string class. In Java, a string is immutable, which means its value cannot be changed once it is created.
The Java double keyword is a primitive data type. This is used to declare the variables and methods. It generally represents the decimal numbers. Java double is used to represent floating-point numbers. It uses 64 bits to store a variable value and has a range greater than float type.
Here, we have used three methods to convert a string to double in the Java programming language.
Convert string to double using the parseDouble() method of Double class
Java parseDouble() method is the part of the Double class of the java.lang package. This method returns a new double initialized to the value represented by the string argument, as done by the valueOf() method of the class Double.
Syntax-double str1 = Double.parseDouble(str);
Example-
// Java program to convert String to Double
// Using parseDouble() Method of Double Class
public class ConvertStrToDouble {
// Driver method
public static void main(String args[])
{
// Create and initializing a string
String str = "1202.893";
// Converting the string into Double
// using parseDouble() Method
double str1 = Double.parseDouble(str);
// prints the double value
System.out.println(str1);
}
}
Output of the above code:
1202.893
Convert string to double using the valueOf() method of Double class
The valueOf(double d) method of Java is a part of the Double class of the java.lang package. This method returns a Double object holding the value represented by the String argument.
Syntax-double str1 = Double.valueOf(str);
Example-
// Java program to convert String to Double
// Using valueOf() Method of Double Class
public class ConvertStrToDouble {
// Driver method
public static void main(String args[])
{
// Create and initializing a string
String str = "1809.72";
// Converting the string to Double type
double str1 = Double.valueOf(str);
// prints the double value
System.out.println(str1);
}
}
Output of the above code:
1809.72
Convert string to double using constructor of Double class
In the given Java program, we have used the constructor of the Double class to convert the string to double. It constructs a newly allocated Double object that represents the floating-point value of type double represented by the string.
Syntax-Double str1 = new Double(str);
Example-
// Java program to convert String to Double
// Using Constructor of Double class
public class ConvertStrToDouble {
// Driver method
public static void main(String args[])
{
// Create and initializing a string
String str = "2309.72";
// Converting above string into double type
Double str1 = new Double(str);
// prints the double value
System.out.println(str1);
}
}
Output of the above code:
2309.72
Related Articles
Java string split multiple delimitersEnum with values in Java
Convert array to list in Java
Java random number between 1 and 100
Calculating percentage in Java
Multiplication table program in Java
Java dialogue box
Fibonacci series using recursion in Java
Java sum of array
Circular prime in Java
Vowel and Consonant program in Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Java program to check leap year
Java program to find factorial of a number