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







