Convert String to double in Java
Today, we'll explore various methods for converting Java strings to double, crucial for handling diverse data types. This conversion, facilitated by autoboxing, seamlessly allows interchangeability between the double primitive type and Double objects without complications.
Example 1: Converting from Double to String Using valueOf() Method in Java
String.valueOf() is an inbuilt method of String class in Java which converts different parameters to string. It works with parameters like object, float, char, double, int, long, and char[]. Since it is an inbuilt method, it is already present in the java directory.
Syntax:
- String output = String.valueOf(parameter);
Parameters: object, float, char, double, int, float, long, char[]
Let's discuss it with an example:
Example 1: The following code converts a double number to a string using the String.valueOf method.
Output:
Example 2 Let's have a look at another example taking a double value as user input.
Output:
In the above example, we have converted a double variable to string using the valueOf() method of the String class. Also, the valueOf() method is the most used way of converting a double variable to a string in Java.
Example 2: Java Program to Convert double to String Using DecimalFormat.format()
Double can be converted to a string using the DecimalFormat.format() method. We can access the DecimalFormat class by importing java.text.DecimalFormat package in the code. It is used to represent a double value as string. We can specify the number of digits that we want after the decimal.
Syntax:
OR
Let's see it with an example:
Example 1: The following example specifies the number of digits after the decimal to be 3.
Output:
Example 2: The following example specifies the number of digits after the decimal to be 4. As the digits after decimal in the original number were less than 4, another zero digits is added to satisfy the formatting.
Output:
Example 3: The following example specifies the digits after decimal to be 2. Since the original number has 3 digits after decimal, which is more than 2, it rounds off the number to two decimal places and then converts it to string.
Output:
Example 4: The above codes can also be written in a different format as:
Output:
Example 3: Java Program to Convert double to string using StringBuffer and StringBuilder.
We can also convert double to string using both StringBuffer and StringBuilder . It first creates an instance, then appends the double value and converts it to a string. The append method accepts a value of any data type as a parameter and adds it to the current object.
Let's explore it using an example.
Syntax:
Parameter: boolean, char, double, float, int, long, string, char[]
Example of converting double to string using StringBuffer:
In this example, we will discuss how to convert double to string using the StringBuffer object. We append a double value to the StringBuffer object using the append() method and get the string value using the toString() method.
Output:
Example of converting double to string using StringBuilder:
In this example, we will discuss how to convert double to string using the StringBuilder object. We append a double value to the StringBuilder object using the append() method and get the string value using the toString() method.
Output:
Example 4: Java Program to Convert double to string using toString()
This is another way to convert double value to string. It stores the primitive double value to a reference variable of double class and changes it to a string using the toString() method.
Syntax:
Example 1: In the following example, we convert the primitive double value by first providing it to the double class reference variable and then converting it using the toString() method.
Output:
Example 2: The double value can be passed directly to the toString() method or it can be taken as an input from a user and passed to the reference variable. Let's have a look at it using an example.
Output:
Here, double is a wrapper class in Java that allows us to convert primitive data types into objects and objects into primitive data types.
A print statement in Java always converts other data types to string using the toString() method of the Object class, whether called directly or not. When a user creates an object of any type, the toString() method gets overridden so that the print command calls the overridden toString() method. If no object is created by the user, the inbuilt function present in the Java directory automatically converts the double value to string.
Example 5: Java Program To Convert double to String Using + Operator
This method uses the concatenation property of strings to convert a variable of double datatype to the string. The + sign concatenates the string to the number changing it to string type.
Syntax:
Example: The following example helps us add numbers to string. The + operator concatenates the string with the number.
Output:
Example 6: Program to Convert double to String using format() method in Java
Double value can be converted to string using the String.format() method. It is used to represent double as a string with a specified number of digits after the decimal.
Syntax:
Example 1:
Output:
Example 2: This method helps us in altering the digits after the decimal in the string or round it off to specified decimal places. Let's explore it through an example.
Output:
The above code helps us in adjusting the number of digits after the decimal to two in the output string. This method helps us in rounding decimals.
Conclusion
Double values automatically get converted to string at the time of printing. If we want to do it for other purposes, we can convert it using the following methods:
- valueOf() - String.valueOf() is an inbuilt method of String class in Java that converts different parameters to strings.
- String.format() - It is used in java to return formatted strings with the specified format and arguments.
- toString() - toString() is an inbuilt function in Java to convert given value to string.
- DecimalFormat.format() - DecimalFormat.format() is a method in java.text.DecimalFormat package that converts double to string with specified number of digits after decimal.
- StringBuffer and StringBuilder - StringBuffer and StringBuilder are classes in Java to create a modifiable chain and sequences of characters using the append() method.
- +operator - The +operator in Java is used to concatenate strings. It uses this property to convert double to string.
Each method can be used for different purposes and places. The methods like String.format() and DecimalFormat.format() are used to alter the digits after decimal places and round off the values to their nearest ones.