Converting double to string
In Java, you can convert a double to a string using the Double.toString()
method or the String.valueOf()
method.
Here is an example using Double.toString()
:
double d = 123.45;
String s = Double.toString(d);
Here is an example using String.valueOf()
:
double d = 123.45;
String s = String.valueOf(d);
You can also use the DecimalFormat
class to specify a particular format for the string representation of the double. Here is an example:
double d = 123.45;
DecimalFormat df = new DecimalFormat("#.##");
String s = df.format(d);
This will produce a string with exactly two decimal places, such as "123.45".