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

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

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

 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; }

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄