java之数据处理,小数点保留位数

1.返回字符串类型,保留后两位:

public static String getRate(Object d) {
        return String.format("%.2f", d);
    }

 

 2.返回字符串类型,保留后两位:

public static String getRate1(Object d) {
        DecimalFormat df = new DecimalFormat();
        df.applyPattern("0.00");
        return df.format(d);
    }

 

3.返回double类型,保留后两位: 

//newScale为小数点位数
public
static Double roundValue(Double d, int newScale) { Double retValue = null; if (d != null) { BigDecimal bd = new BigDecimal(d); retValue = bd.setScale(newScale,BigDecimal.ROUND_HALF_UP).doubleValue(); } return retValue; }

 

posted @ 2019-05-13 10:19  bitbetter  阅读(5813)  评论(0编辑  收藏  举报