Examples
- How to Convert Char to String in Java? Character to String
- How to Convert Char to Int in Java? Character to Integer Conversion
- How to Convert Int to Char in Java? Integer to Character
- How to Convert Long to Int in Java? Long to Integer
- How to Convert Int to Long in Java? Integer to Long Program
- How to Convert Boolean to String in Java? (Parse Bool to String)
- How to Convert String to Boolean in Java? 3 Ways With Program
- How to Convert String to Int in Java? String to Integer Program
- How to Convert Int to String in Java? Integer to String Program
- How to Convert Int to Double in Java? (Integer to Double)
- How to Convert Double to Int in Java? 4 Ways
- How to Convert String to Double in Java? Programs
- How to Convert Double to String in Java? Programs
How to Convert Double to String in Java? Programs
Data conversion is a common and essential task in Java programming. Whether you're building a financial application, scientific software, or even a simple calculator, there will come a time when you need to convert double to String in Java. This is crucial for displaying numeric data, storing it in files, or passing it as part of a message in a Java application.
This tutorial will walk you through the various methods and techniques available to convert a double to a String in Java effectively.
We'll explore the built-in Java methods, such as String.valueOf(), Double.toString(), and String.format(), and discuss when and how to use each one.
Concepts to Learn:
Double to String in Java Using String.valueOf()
Here's a Java program to convert a double to a String using the String.valueOf(double) method:
Code
public class DoubleToStringExample {
public static void main(String[] args) {
// Define a double value
double doubleValue = 123.456;
// Convert the double to a string using String.valueOf()
String stringValue = String.valueOf(doubleValue);
// Display the original double and the converted string
System.out.println("Original double value: " + doubleValue);
System.out.println("Converted string value: " + stringValue);
}
}
Output
Original double value: 123.456
Converted string value: 123.456
Explanation
-
We start by defining a double variable named doubleValue with a value of 123.456.
-
Next, we use the String.valueOf(double) method to convert the double value to a String. This method takes a double as input and returns a String representation of that double.
-
We store the converted String in a variable called stringValue.
-
Finally, we print out both the original double value and the converted String value using System.out.println() statements.
Java Double to String Using Double.toString()
Here's a program to convert double to String in Java programming using the Double.toString(double) method:
Code
public class DoubleToStringExample {
public static void main(String[] args) {
// Define a double value
double doubleValue = 789.123;
// Convert the double to a string using Double.toString()
String stringValue = Double.toString(doubleValue);
// Display the original double and the converted string
System.out.println("Original double value: " + doubleValue);
System.out.println("Converted string value: " + stringValue);
}
}
Output
Original double value: 789.123
Converted string value: 789.123
Explanation
-
We start by defining a double variable named doubleValue with a value of 789.123.
-
Next, we use the Double.toString(double) method to convert the double value to a String. This method is a static method of the Double class and takes a double as input, returning a String representation of the double.
-
We store the converted String in a variable called stringValue.
-
Finally, we print out both the original double value and the converted String value using System.out.println() statements.
Convert Doube to String in Java Using String.format()
Here's a program on how to convert double to String in Java using the String.format() method:
Code
public class DoubleToStringExample {
public static void main(String[] args) {
// Define a double value
double doubleValue = 123.456;
// Convert the double to a string using String.format()
String stringValue = String.format("%.2f", doubleValue);
// Display the original double and the converted string
System.out.println("Original double value: " + doubleValue);
System.out.println("Converted string value: " + stringValue);
}
}
Output
Original double value: 123.456
Converted string value: 123.46
Explanation
-
We start by defining a double variable named doubleValue with a value of 123.456.
-
Next, we use the String.format() method to convert the double value to a formatted String. In this example, we specify "%.2f" as the format string. %.2f is a format specifier that formats the double with two decimal places.
-
We store the formatted String in a variable called stringValue.
-
Finally, we print out both the original double value and the converted formatted String value using System.out.println() statements.
Convert Double to String in Java Using DecimalFormat class
To convert a double to a String using the DecimalFormat class in Java, you can follow this example. The DecimalFormat class provides more advanced formatting options than some other methods:
Code
import java.text.DecimalFormat;
public class DoubleToStringExample {
public static void main(String[] args) {
// Define a double value
double doubleValue = 789.123;
// Create a DecimalFormat instance with the desired format
DecimalFormat decimalFormat = new DecimalFormat("#0.00");
// Convert the double to a string using DecimalFormat
String stringValue = decimalFormat.format(doubleValue);
// Display the original double and the converted string
System.out.println("Original double value: " + doubleValue);
System.out.println("Converted string value: " + stringValue);
}
}
Output
Original double value: 789.123
Converted string value: 789.12
Explanation
-
We start by defining a double variable named doubleValue with a value of 789.123.
-
We create an instance of the DecimalFormat class, named decimalFormat, with the desired format. In this case, "#0.00" is used as the format pattern. This format pattern specifies that we want two decimal places.
-
We use the decimalFormat.format(doubleValue) method to convert the double value to a formatted String. The format method formats the double value according to the pattern specified when creating the DecimalFormat instance.
-
We store the formatted String in a variable called stringValue.
-
Finally, we print out both the original double value and the converted formatted String value using System.out.println() statements.
Java Double to String Using StringBuilder or StringBuffer
To convert a double to a String using the StringBuilder or StringBuffer concatenation method in Java, you can follow this example. This method involves creating a StringBuilder or StringBuffer and appending the double value as a string:
Code
public class DoubleToStringExample {
public static void main(String[] args) {
// Define a double value
double doubleValue = 123.456;
// Create a StringBuilder instance
StringBuilder stringBuilder = new StringBuilder();
// Append the double value as a string
stringBuilder.append(doubleValue);
// Convert the StringBuilder to a string
String stringValue = stringBuilder.toString();
// Display the original double and the converted string
System.out.println("Original double value: " + doubleValue);
System.out.println("Converted string value: " + stringValue);
}
}
Output
Original double value: 123.456
Converted string value: 123.456
Explanation
-
We start by defining a double variable named doubleValue with a value of 123.456.
-
We create a StringBuilder instance named stringBuilder. Alternatively, you can use StringBuffer if you require thread safety.
-
We use the append() method of the stringBuilder to add the double value as a string to the StringBuilder.
-
We convert the StringBuilder to a String by calling the toString() method, storing the result in a variable called stringValue.
-
Finally, we print out both the original double value and the converted String value using System.out.println() statements.
Learn Next: