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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM