一、Java保留两位小数方式
public static void main(String[] args) {
System.out.println("=======DecimalFormat(四舍五入,五入可能失败)=========");
DecimalFormat decimalFormat = new DecimalFormat("0.00");
System.out.println(decimalFormat.format(0.235)); //0.23
System.out.println(decimalFormat.format(1.235)); //1.24
System.out.println(decimalFormat.format(2.235)); //2.23
System.out.println(decimalFormat.format(3.235)); //3.23
System.out.println(decimalFormat.format(4.235)); //4.24
System.out.println("保留n为小数,n>=1时和js的toFixed一致");
System.out.println("=======DecimalFormat(默认:ROUND_HALF_EVEN)");
decimalFormat = new DecimalFormat("0");
System.out.println(decimalFormat.format(0.5)); //0
System.out.println(decimalFormat.format(1.5)); //2
System.out.println(decimalFormat.format(2.5)); //2
System.out.println(decimalFormat.format(3.5)); //4
System.out.println("不保留小数,向偶数靠近,与toFixed不一致");
System.out.println("=======String.format(四舍五入)=========");
System.out.println(String.format("%.2f",0.235)); //0.24
System.out.println(String.format("%.2f",1.235)); //1.24
System.out.println(String.format("%.2f",2.235)); //2.24
System.out.println(String.format("%.2f",3.235)); //3.24
System.out.println(String.format("%.0f",0.5)); //1
System.out.println(String.format("%.0f",1.5)); //2
System.out.println(String.format("%.0f",2.5)); //3
System.out.println(String.format("%.0f",3.5)); //4
System.out.println("=======NumberFormat(和DecimalFormat一致)=========");
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
System.out.println(nf.format(0.235)); //0.23
System.out.println(nf.format(1.235)); //1.24
System.out.println(nf.format(2.235)); //2.23
System.out.println(nf.format(3.235)); //3.23
System.out.println(nf.format(4.235)); //4.24
nf.setMaximumFractionDigits(0);
System.out.println(nf.format(0.5)); //0
System.out.println(nf.format(1.5)); //2
System.out.println(nf.format(2.5)); //2
System.out.println(nf.format(3.5)); //4
System.out.println(nf.format(4.5)); //4
System.out.println("=======BigDecimal(指定舍入模式,构造函数使用String类型方可精确计算)=========");
System.out.println("ROUND_HALF_DOWN:五舍五点1入");
//BigDecimal bigDecimal = new BigDecimal(1.235); //直接使用double类型计算不对
// System.out.println(bigDecimal.setScale(2,BigDecimal.ROUND_HALF_DOWN).doubleValue());
BigDecimal bigDecimal = new BigDecimal(String.valueOf(0.235));
System.out.println(bigDecimal.setScale(2,BigDecimal.ROUND_HALF_DOWN).doubleValue()); //0.23
bigDecimal = new BigDecimal(1.235+"");
System.out.println(bigDecimal.setScale(2,BigDecimal.ROUND_HALF_DOWN).doubleValue()); //1.23
bigDecimal = new BigDecimal(1.2351+"");
System.out.println(bigDecimal.setScale(2,BigDecimal.ROUND_HALF_DOWN).doubleValue()); //1.24
System.out.println("ROUND_HALF_UP:四舍五入");
//bigDecimal = new BigDecimal(0.235); //直接使用double类型计算不对
// System.out.println(bigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
bigDecimal = new BigDecimal(String.valueOf(0.235));
System.out.println(bigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue()); //0.24
bigDecimal = new BigDecimal(1.235+"");
System.out.println(bigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue()); //1.24
}
二、js保留两位小数
在Google Chrome和Safari浏览器测试的结果
0.235.toFixed(2); //0.23
1.235.toFixed(2); //1.24
2.235.toFixed(2); //2.23
3.235.toFixed(2); //3.23
4.235.toFixed(2); //4.24
0.5.toFixed(0); //1
1.5.toFixed(0); //2
2.5.toFixed(0); //3
3.5.toFixed(0); //4
总结: