import java.text.DecimalFormat;
import java.text.NumberFormat;
public class TestNumberFormat {
public static void main(String[] args){
Double d = 5.0123456789;
DecimalFormat nf = new DecimalFormat();
nf.setMaximumFractionDigits(2);
System.out.println(nf.format(d));
}
}
结果:
5.01