public static void main(String[] args){
System.out.println(getFormatNum(“0.000”,1.2224));
System.out.println(getFormatNum(“0.000”,1.2225));
System.out.println(getFormatNum(“0.000”,1.2226));
System.out.println(getFormatNum(“0.000”,0.2224));
System.out.println(getFormatNum(“0.000”,0.2225));
System.out.println(getFormatNum(“0.000”,0.2226));
System.out.println(getFormatNum(“0.000”,2.4924));
System.out.println(getFormatNum(“0.000”,2.4925));
System.out.println(getFormatNum(“0.000”,2.4926));
}
System.out.println(getFormatNum(“0.000”,1.2224));
System.out.println(getFormatNum(“0.000”,1.2225));
System.out.println(getFormatNum(“0.000”,1.2226));
System.out.println(getFormatNum(“0.000”,0.2224));
System.out.println(getFormatNum(“0.000”,0.2225));
System.out.println(getFormatNum(“0.000”,0.2226));
System.out.println(getFormatNum(“0.000”,2.4924));
System.out.println(getFormatNum(“0.000”,2.4925));
System.out.println(getFormatNum(“0.000”,2.4926));
}
public static Double getFormatNum(String pattren,Double data){
String reStr = “”;
reStr = new java.text.DecimalFormat(pattren).format(data);
return Double.parseDouble(reStr);
}
为什么执行结果会是
1.222
1.222
1.223
0.222
0.223
0.223
2.492
2.493
2.493
当个位为1时并没有按照四舍五入的规则执行
解决方案
20
有问题。好像全是1.223了。等会儿啊,再研究研究