zoukankan      html  css  js  c++  java
  • java保留两位小数和js保留两位小数一致性研究

    一、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模式  精确四五五入,结果也一致

    由于浏览器的多样性,数据的无限性,没有做过多测试,这只是初步结论。要确保万无一失,还是由一处计算方为上策!

  • 相关阅读:
    Android 8.0编译过程
    Ubuntu下映射网络驱动器
    Android 指定调用已安装的某个“相机”App
    sendMessage 与 obtainMessage (sendToTarget)比较
    Linux tee命令
    Android P(9.0) userdebug版本执行adb remount失败
    adb shell get/setprop, setenforce...
    Bluedroid: 蓝牙协议栈源码剖析
    android o logcat read: unexpected EOF!
    Winform 打包 混淆 自动更新
  • 原文地址:https://www.cnblogs.com/hdwang/p/9106412.html
Copyright © 2011-2022 走看看