Java Convert String to double17 Mar 2025 | 4 min read To convert a string to a double in Java, one must first convert the string's numerical value from a series of characters to a double-precision floating-point representation. When working with user input or data from outside sources, where values are initially in string format, this conversion is commonly required. Double.parseDouble() is one of the built-in Java methods that help you complete this work quickly and accurately. It is imperative to manage possible exceptions, such as NumberFormatException, which can arise in the event that the string fails to represent a legitimate numerical value. Java programs can process and manipulate numerical data across a wide range of applications and use cases by correctly converting strings to doubles. ![]() ScenarioIt is generally used if we have to perform mathematical operations on the string that contains double number. Whenever we get data from textfield or textarea, entered data is received as a string. If entered data is double, we need to convert string to double. To do so, we use Double.parseDouble() method. SignatureThe parseDouble() is the static method of Double class. The signature of parseDouble() method is given below: 1. Using Double.parseDouble() methodWe can convert String to double in Java using Double.parseDouble() method Let's see the simple example of converting String to double in Java. Filename: StringToDoubleExample,java Test it NowOutput: 23.6 Explanation Here's an example of using the Double.parseDouble() function to turn the decimal number "23.6" from a text representation into a double in Java code. The numerical value is stored in the string variable s. Double.parseDouble() parses the string value to get the matching double value, which is then assigned to the variable d. The double value d is finally printed to the console by the program. This is a typical method for turning strings into doubles in Java, which is essential for managing numbers in applications. 2. Using Double.valueOf() MethodWe can also convert a String to double in Java using Double.valueOf() method. This method returns the Double object representing the specified string when parsed with the value of a double. It's useful when we need the Double object for further processing rather than just the primitive double value. Signature This is a static method of the Double class. It's commonly utilized when we want to handle exceptions using try-catch blocks or when we need to perform additional operations with the Double-object before obtaining the primitive double value. Here's an example demonstrating the conversion of a string to double using Double.valueOf() method: This code snippet converts the string "23.6" to a Double object using Double.valueOf() and then obtains the primitive double value from it. It's important to note that this method may throw a NumberFormatException if the string does not contain a parsable double. Filename: StringToDoubleExample.java Output: 23.6 Explanation The following Java code initialises a string variable called s with the value "23.6" and uses Double.valueOf() to transform it into a Double object called dObj. Next, it uses doubleValue() to get the primitive double value from dObj and assigns it to the variable d. Ultimately, the code outputs the double value d to the console, thereby illustrating how to use Java's Double.valueOf() function to convert a text to a double. 3. Using NumberFormat.parse() MethodThe NumberFormat class can be used to parse a number from a string representation into a Number object, which can subsequently be transformed into a double. Greater flexibility in managing input particular to a given locale and parsing various number formats is offered by this method. Na umberFormat's parse() function yields a Number object that represents the value that has been parsed. The primitive double value can then be obtained using the doubleValue() function. Filename: StringToDoubleExample.java Output: 23.6 Explanation This code initialises a string variable called s with the value "23.6" and then uses NumberFormat.parse() to parse the string into a Number object. In the end, it prints the primitive double value to the terminal after retrieving it from the Number object. When handling multiple number formats or parsing input from different locales, this approach comes in handy. Next TopicJava double to String |
Java Convert String to Object We can convert a String to an Object in Java with assignment operator. Each class is internally a child class of the Object class. So, we can assign string to Object directly. We can also convert String to Class type object using the...
7 min read
Java Convert Converting a decimal number to binary is a fundamental operation in computer science and programming. Binary is a base-2 numeral system, meaning it only uses two digits, 0 and 1. In contrast, the decimal system, which we use daily, is a base-10 numeral system...
17 min read
Java Convert long to int Converting a long to an int results in potential loss of data due to a long can store larger values than an int. Java has various ways of doing so, and you must select the best way based on your particular needs....
12 min read
Java Convert int to double Converting data types is a common operation in Java programming, especially when dealing with numerical data. One such Conversion is from int (integer) to double. Conversion is crucial as it enhances accuracy in calculations and processes involving fractional numbers. The int data...
3 min read
Java Convert int to long Data types play an important role in managing and manipulating information in Java programming. These have base numbers int and length for storing integer values. Although int is mostly used for small integer values, longer steps are taken to handle larger ranges....
3 min read
Java Convert int to char We can convert int to char in Java using typecasting. In order to convert higher data type into lower data type, we need to perform typecasting. Here, the ASCII character of integer value will be stored in the char variable. To get...
7 min read
In Java, the conversion of a String variable into an int variable is considered a common process or operation. It may occur when the user's input is processed or manipulated. We can convert a String to an int in Java using the Integer.parseInt() and Integer.valueOf()...
6 min read
Java Convert Hexadecimal to Decimal Converting hexadecimal to decimal (base 10) in Java can be done using the Integer.parseInt() method or by implementing custom logic. This conversion is often needed when working with low-level data representations, color codes, and memory addresses. Java Hexadecimal to Decimal conversion: Using...
4 min read
Java Convert The conversion of a Timestamp to a Date in Java is considered a simple and straightforward process. The Timestamp can be utilized for representing a particular moment of time which is majorly used for operations related to database. Whereas, a Date can be utilized...
5 min read
Java Convert adecimal We can convert decimal to hexadecimal in java using Integer.toHexString() method or custom logic. Java conversion: Integer.toHexString() The Integer.toHexString() method converts decimal to hexadecimal. The signature of toHexString() method is given below: public static String toHexString(int decimal) Let's see the simple example of converting decimal to binary...
1 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India