BigDecimal類型數據保留兩位小數即百分比運算


方法示例:



DecimalFormat df = new DecimalFormat("0.00");

Object price = 2;
Object price1 = 2.3;
Object price2 = 2.3154;

String str = df.format(price);
String str1 = df.format(price1);
String str2 = df.format(price2);

System.out.println("str-->"+str ) ; // 結果為 str = 2.00
System.out.println("str1-->"+str1 ) ; // 結果為 str1 = 2.30
System.out.println("str2-->"+str2 ) ; // 結果為 str2 = 2.32


BigDecimal bd = new BigDecimal(str);
BigDecimal bd1 = new BigDecimal(str1);
BigDecimal bd2 = new BigDecimal(str2);

System.out.println(bd); // 結果為 2.00
System.out.println(bd1); // 結果為 2.30
System.out.println(bd2); // 結果為 2.32

 


免責聲明!

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



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