1.问题:
数字格式:四舍五入的用法;
2.源代码:
public class TestDemo4 {
public static void main(String[] args) {
NumberFormat nf = NumberFormat.getPercentInstance();/*设置为百分比*/
nf.setMinimumFractionDigits(2);//设置该百分比数字,保留2位小数;
nf.setRoundingMode(RoundingMode.HALF_UP); //设置满5向上进位,即四舍五入;
/*实验*/
Double a=0.1956565445;
String b=nf.format(a);
System.out.println(b);
}
}
3.截图:
4.总结:
①设置百分比:
NumberFormat nf=NumberFormat.getPercentInstance();
②设置百分比保留的位数:
nf.setMinimumFractionDigits(2);
③进位规则:
nf.setRoundingMode(RoundingMode.HALF_UP);
满5向上进位;还有其它方法,例如满5向下进位;