美文网首页
BigDecimal的问题

BigDecimal的问题

作者: 吉凶以情迁 | 来源:发表于2019-06-22 16:22 被阅读0次
    100.386->ROUND_HALF_EVEN 100.39
    100.386->ROUND_HALF_UP 100.39
    100.386->ROUND_HALF_DOWN 100.39
    100.386->ROUND_UP 100.39
    100.386->ROUND_DOWN 100.38
    100.386->ROUND_FLOOR 100.38
    100.386->ROUND_CEILING 100.39
    ------------
    100.382->ROUND_HALF_EVEN 100.38
    100.382->ROUND_HALF_UP 100.38
    100.382->ROUND_HALF_DOWN 100.38
    100.382->ROUND_UP 100.39
    100.382->ROUND_DOWN 100.38
    100.382->ROUND_FLOOR 100.38
    100.382->ROUND_CEILING 100.39
    ------------
    100.38->ROUND_HALF_EVEN 100.38
    100.38->ROUND_HALF_UP 100.38
    100.38->ROUND_HALF_DOWN 100.38
    100.38->ROUND_UP 100.38
    100.38->ROUND_DOWN 100.37
    100.38->ROUND_FLOOR 100.37
    100.38->ROUND_CEILING 100.38
    ------------
    100.38->ROUND_HALF_EVEN 100.38
    100.38->ROUND_HALF_UP 100.38
    100.38->ROUND_HALF_DOWN 100.38
    100.38->ROUND_UP 100.38
    100.38->ROUND_DOWN 100.37
    100.38->ROUND_FLOOR 100.37
    100.38->ROUND_CEILING 100.38
    ------------
    
       BigDecimal bg = new BigDecimal(money);
    //        double f1 = bg.setScale(2, BigDecimal.ROUND_FLOOR).doubleValue();//抹掉后面的。
    //        return f1;
    
      String format = String.format("money %.2f", 100.356f);//100.36
            DecimalFormat df = new DecimalFormat("#.00");
            System.out.println("test:" + df.format(100.356f));
            System.out.println(format);
    
    
            NumberFormat nf = NumberFormat.getNumberInstance();
            nf.setMaximumFractionDigits(2);
            double f = 100.356f;
            System.out.println(nf.format(f));
    
    
    
    
    

    // 经过测试系统的api,无论哪种只保留2位小数,第三位没有或者存在 都会导致出现不同的结果, 需求是不受后面有没有或者存在的影响,所以写了这个东西。

        public  static String keepFormatNoney(Object money, int keepCount) {
            String moneyStr = money + "";
            int index = moneyStr.indexOf(".");
            if (index == -1) {
                return moneyStr;
            } else {
                int end;
                if ((index + keepCount + 1) > moneyStr.length()) {
                    end = moneyStr.length();//index+2>s.length()?index+2:s.length()
                } else {
                    end = index + keepCount + 1;
    
                }
                String substring = moneyStr.substring(0, end);
                return substring;
            }
        }
    
            System.out.println("final str " + keepFormatNoney(10.333,2));
            System.out.println("final str " + keepFormatNoney(10.32345,2));
            System.out.println("final str " + keepFormatNoney(103.3,3));
            System.out.println("final str " + keepFormatNoney(103.3333,3));
    
    final str 100.12
    final str 10.33
    final str 10.32
    final str 103.3
    final str 103.333
    
    

    小数点 乘上100保留3位数的办法

     public static String formatMoney(float money) {
            int mymoney = (int) (money * 100);
            String format = String.format("%3d", mymoney);
            return format.replace(" ", "0");
            /*if(money<10){
                return  "00"+money;
            }else{
                return money+"";
            }*/
        }
    

    然而还是有问题
    修正后

       public static String formatMoney(double money) {
           BigDecimal bigDecimal = new BigDecimal(Double.toString(money));
           int mymoney = bigDecimal.multiply(new BigDecimal(Double.toString(100))).intValue();
         /*  bigDecimal = new BigDecimal(Double.toString(mymoney));
           bigDecimal.setScale(2, BigDecimal.ROUND_DOWN);
           double reverseValue = bigDecimal.divide(new BigDecimal(Double.toString(100))).doubleValue();
    
           if (reverseValue != money) {
               throw new RuntimeException("rever error:" + moneyStr + ",doResult:" + reverseValue);
           }*/
           String format = String.format("%3d", mymoney);
           return format.replace(" ", "0");
       }
       public static String formatMoney(String moneyStr) {
           double money = Double.parseDouble(moneyStr);
           return formatMoney(money);
       }
    
    

    相关文章

      网友评论

          本文标题:BigDecimal的问题

          本文链接:https://www.haomeiwen.com/subject/vfrvqctx.html