計算評價值,小數位不爲零保留兩位小數
/**
* 計算評價值,小數位不爲零保留兩位小數
*
* @param totalAmount 分母
* @param realityAmount 分子
* @return String
*/
public String getRate(Integer totalAmount, Integer realityAmount) {
if (totalAmount == 0) {
return "0";
}
// 創建一個數值格式化對象
NumberFormat numberFormat = NumberFormat.getInstance();
// 設置精確到小數點後2位
numberFormat.setMaximumFractionDigits(2);
return numberFormat.format((float) realityAmount / (float) totalAmount * 100);
}