Converting data types in Java is essential for ensuring data integrity and accurate program functionality. Common scenarios requiring string-to-double conversion include processing user input, parsing data from files, and working with APIs. This blog will explore the methods and best practices for converting strings to doubles in Java.
6 Methods to Convert Double to String in Java
Convert Double to String in Java Using StringBuilder.append() method
Below java code exemplifies the conversion of a `double` value to a `String` using `StringBuilder`:
// Java Program to Convert Double to String public class convertDoubleToString { public static void main(String[] args) { double number = 789.136; // Double value // Using StringBuilder to convert double to String StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(number); // Append double to StringBuilder String convertedString = stringBuilder.toString(); // Convert StringBuilder to String System.out.println("Double value as String: " + convertedString); } }
Explanation of the Code:
- Firstly, a `double` value, “number” (set as 789.136), is assigned. The `StringBuilder` class is employed to convert this `double` to a `String`.
- Through the `append()` method, the `double` value is added to the `StringBuilder`. Subsequently, invoking `toString()` on the `StringBuilder` retrieves the `String` representation of the `double`.
- Finally, printing the converted `String` displays “789.136”, the original `double` value transformed into a `String` using the `StringBuilder` approach.
Output:
Double value as String: 789.136
Convert Double to String in Java Using StringBuffer.append() method
Below provided Java code showcases the conversion of `float` values to `String` using the `StringBuffer` and `append()` methods:
// Java Program to Convert Double to String public class convertDoubleToString { public static void main(String[] args) { String str = (new StringBuffer()).append(87.869f).toString(); System.out.println(str); str = (new StringBuffer()).append(-54.9638571f).toString(); System.out.println(str); } }
Explanation of the Code:
- Two `float` numbers, 87.869 and -54.9638571, are appended to separate `StringBuffer` instances and then converted to strings using the `toString()` method.
- When printed, the output displays the converted `String` representations of these `float` values.
- However, due to precision limitations in floating-point arithmetic, the second number experiences a slight deviation in the output due to rounding, resulting in -54.963856 being displayed instead of the original value -54.9638571.
Output:
87.869
-54.963856
Convert Double to String in Java Using String.format() method
Check out given Java code showcases the conversion of a `double` value, “8945.2,” to a `String` with a specific format:
// Java Program to Convert Double to String public class convertDoubleToString { public static void main(String[] args) { double number = 8945.2; // Double value // Using String.format() to convert double to string with specific format String formattedString = String.format("%.2f", number); System.out.println("Formatted String: " + formattedString); } }
Explanation of the Code:
- Initially, the `double` value is assigned to the variable “number.”
- The `String.format()` method is employed, formatting the `double` value with “%.2f,” which specifies two decimal places.
- This creates a new `String` named “formattedString” representing the `double` value in the specified format.
- The program then prints this formatted `String` using `System.out.println()`. Upon execution, the output displays “Formatted String: 8945.20,” presenting the `double` value as a `String` with two decimal places as per the defined format.
Output:
Formatted String: 8945.20
Convert Double to String in Java Using the “+” operator
Java code demonstrates the conversion of a `double` value to a `String`:
// Java Program to Convert Double to String import java.io.*; import java.util.*; public class convertDoubleToString { public static void main(String[] args) { double num = 985.67; // Example double value // Using the "+" operator for concatenation to convert double to string String convertedString = "" + num; // Display the converted string System.out.println("Converted String: " + convertedString); } }
Explanation of the Code:
- Initially, a `double` value `num` (here, 985.67) is declared. Utilizing the concatenation operation (`”” + num`), the code converts the `double` value to a `String` by implicitly invoking the `String.valueOf()` method or performing string concatenation.
- After the conversion, the resulting `String` representation of the `double` value is stored in the variable `convertedString`.
- Finally, using `System.out.println()`, the program displays the converted `String` representation of the `double` value.
- Upon execution, the output showcases the `double` value converted to a `String`, resulting in “Converted String: 985.67”.
- This approach leverages string concatenation for a straightforward `double` to `String` conversion in Java.
Output:
Converted String: 985.67
Convert Double to String in Java Using valueOf()
Code shows the conversion of a `double` value, ‘846.475’, to a `String`:
// Java Program to Convert Double to String import java.io.*; import java.util.*; public class convertDoubleToString { // Main driver function public static void main(String[] args) { // Declaring and initializing double number double num = 846.475; // Converting Double data to String data String output = String.valueOf(num); // Printing the above string System.out.println(output); } }
Explanation of the Code:
- Initially, the `double` variable ‘num’ is declared and initialized with the value ‘846.475’.
- The code uses `String.valueOf()` to convert the `double` value to its equivalent `String` representation, assigning it to the ‘output’ variable.
- Finally, `System.out.println()` is used to display the converted `String` value.
- Upon execution, the output showcases the successful conversion of the `double` value ‘846.475’ to a `String`, resulting in the output displaying ‘846.475’.
- The code effectively demonstrates how to convert a `double` data type to its respective `String` representation in Java using the `String.valueOf()` method.
Output:
846.475
Convert Double to String in Java Using Double.toString() method
Java code below demonstrates the conversion of `double` values to `String`.
// Java Program to Convert Double to String public class convertDoubleToString { public static void main(String[] args) { String s1 = Double.toString(894.54); String s2 = Double.toString(547.6); String s3 = Double.toString(5.9); System.out.println(s1); System.out.println(s2); System.out.println(s3); } }
Explanation of the Code:
- Three `double` values (894.54, 547.6, and 5.9) are converted into their respective `String` representations using the `Double.toString()` method.
- Each converted `String` is stored in variables `s1`, `s2`, and `s3`.
- Upon execution, the code prints the converted `String` representations of the `double` values using `System.out.println()`.
- Consequently, the output showcases each `double` value converted to its equivalent `String`, displaying 894.54, 547.6, and 5.9 on separate lines, respectively.
- The `Double.toString()` method efficiently converts `double` values to their textual representations for easy manipulation or display purposes.
Output:
894.54
547.6
5.9
We hope you find our blog on “Convert Double to String in Java” informative. In Java, accurately converting doubles to strings is vital for various applications. Utilizing methods like `String.valueOf()` and `Double.toString()`, along with formatting options like `DecimalFormat` and `String.format()`, ensures precision and control over the converted string representation. This blog covered essential methods and best practices. Explore more resources on Newtum, including blogs and courses on Java, HTML, C, and other languages. Happy Coding!