I\'d like to vary the precision of a double representation in a string I\'m formatting based on user input. Right now I\'m trying something like:
String foo
You can use the DecimalFormat class.
double d1 = 3.14159; double d2 = 1.235; DecimalFormat df = new DecimalFormat("#.##"); double roundedD1 = df.format(d); // 3.14 double roundedD2 = df.format(d); // 1.24
If you want to set the precision at run time call:
df.setMaximumFractionDigits(precision)