How to Convert String variables to double in Java Program
July 28, 2023
Java Strings
A String object is an instance of the java.lang.String class, which is immutable and final. This means that once a String object is created, its value cannot be changed.
you can create new strings by concatenating existing strings or by creating a new string that is a substring of an existing string.
Strings in Java are immutable, which means that once you create a string object, its value cannot be changed.
You must be familiar with following topics to understand the correspond example Such as: Java Strings, Java Data Types.
Steps to convert string variables to double
Here are the steps to convert string variables to double in Java:, You can follow these steps:
Store the string representation of a number in a String variable.
Use the Double.parseDouble() method to convert the string to a double. The method takes the string representation of a number as its argument and returns the equivalent double value.
Store the result of the Double.parseDouble() method in a double variable.
If the string passed to Double.parseDouble() cannot be successfully parsed into a double, a NumberFormatException will be thrown.
To handle this exception, you can wrap the call to Double.parseDouble() in a try-catch block.
Java Data Types :
In Java, data types are primarily used to specify the kinds of values that a variable may store. A variable must be given a data type when it is declared, which establishes the kinds of values it can hold. The Java compiler uses this data to allocate memory for the variable and to enforce type-checking while the programme is being executed.
Java Strings
String is the most commonly used data type for processing and manipulating text data. It provides a range of methods for performing various tasks such as concatenating, searching, replacing, and converting text data.
public class Main {
public static void main(String[] args) {
String numberAsString = "1234.56";
double numberAsDouble = Double.parseDouble(numberAsString);
System.out.println("The number as a double: " + numberAsDouble);
}
}
Output
The number as a double: 1234.56
Explanation:
The Double.parseDouble() method takes a string representation of a number and returns the equivalent double value.
In this example, the string "1234.56" is passed as an argument to the parseDouble() method.
The result is stored in the numberAsDouble variable.
The value of numberAsDouble is then printed to the console using System.out.println().
Example 2 :Java Program to convert string variables to double
public class Main{
public static void main(String args[]){
String str = "809.989";
/* Convert String to double using
* parseDouble(String) method of Double
* wrapper class
*/
double dnum = Double.parseDouble(str);
//displaying the value of variable dnum
System.out.println(dnum);
}
}
Output
809.989
Explanation:
The code takes a string "809.989" and converts it to a double using Double.parseDouble().
The resulting double value is stored in the dnum variable, and then displayed to the console using System.out.println().
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment