一、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
总结:
1.当保留至少一位小数时,js的toFixed方式与java的DecimalFormat和NumberFormat方式一致,结果不确定,四舍五可能不入!当不保留小数时,两种算法不一致,js四舍五入,java四舍五取偶。
2.java可以做到精确的四五五入可采用String.format方式和BigDecimal的ROUND_HALF_UP方式,但是必须确保BigDecimal的构造参数为String类型,否则也会发生五不入现象。
3.js精确四舍五入实现方法,网上找到的例子,如下
//覆盖默认的toFixed函数
Number.prototype.toFixed = function(s)
{
return (parseInt(this * Math.pow(10,s) + 0.5)/Math.pow(10,s)).toString();
}
所以,如果保持前后台计算一致,则有如下方案
前端 | 后台 | 结果 |
toFixed(n); n>=1 | DecimalFormat or NumberFormat | 不精确四舍五入,但结果一致 |
toFixed(0); | String.format("%.0f",0.5) 或者 BigDecimal使用ROUND_HALF_UP模式 | 精确四舍五入,结果一致 |
改进后的toFixed | String.format 或者 BigDecimal使用ROUND_HALF_UP模式 | 精确四五五入,结果也一致 |
由于浏览器的多样性,数据的无限性,没有做过多测试,这只是初步结论。要确保万无一失,还是由一处计算方为上策!
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx
本文永久更新链接地址:https://www.linuxidc.com/Linux/2018-06/152737.htm