Create an instance of DecimalFormat with the required format and use its format() method to format the double value. See the DecimalFormat javadoc for details on specifying format.
double d = 1.23456789;
// Use 2 decimal places
NumberFormat numberFormat =
new DecimalFormat("#.##");
String s = numberFormat.format(d);
System.out.println(s); // Outputs: 1.23
written by objects
\\ tags: double, format, string
Use static method Double.toString()
double d = 123.456;
String s = java.lang.Double.toString(d);
// An alternative would be to
// use the following string concatenation
String s2 = "" + d;
written by objects
\\ tags: conversion, double, string
The Double class contains a number of (static) methods for converting to/from various formats. To convert to a double use static method Double.parseDouble()
String s = "123.456";
try
{
double d = Double.parseDouble(s);
}
catch (NumberFormatException ex)
{
// s is not a double
}
written by objects
\\ tags: conversion, double, string
Recent Comments