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