两个整数相除得得到结果百分比【我】

 

 

public static String divi(Integer a,Integer b){
        //如果b为0,这里近似为1
        if (b==0){
            if (a>0){
                return "100";
            }else if (a<0){
                return "-100";
            }else{
                return "0";
            }
        }
        //(两位小数百分比)
        double finalProfitRate = a / b.doubleValue()*100;
        double v = Math.round(finalProfitRate * 100) / 100.0;
        //去掉double类型乘以100后后面多余的.0
        String  s = String.valueOf(v);
        if(s.indexOf(".")>0){
            s = s.replaceAll("0+?$","");
            s = s.replaceAll("[.]$","");
        }
        return s;
    }

 

posted @ 2021-11-08 17:45  戈博折刀  阅读(240)  评论(0编辑  收藏  举报