Possible Duplicate:
Round a double to 2 significant figures after decimal point
The code below works
import java.text.DecimalFormat;
public class Test {
public static void main(String[] args){
double x = roundTwoDecimals(35.0000);
System.out.println(x);
}
public static double roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.00");
twoDForm.setMinimumFractionDigits(2);
return Double.valueOf(twoDForm.format(d));
}
}
it results to 35.0.
How to forced the minimum number of decimal places?
The output I want is 35.00
This isn’t working like you expect because the return value of
roundTwoDecimals()is of typedouble, which discards the formatting you do within the function. In order to achieve what you want, you might consider returning theStringrepresentation fromroundTwoDecimals()instead.