/** * 對數字字符串 不 四舍五入處理 * * @param str 處理參數 * @param scale 保留小數位數 * @return 返回值 */ public class RoundNoOfUtil { public static String RoundNoOf(String str, int scale) { try { // 輸入精度小於0則拋出異常 if (scale < 0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); } // 取得數值 BigDecimal b = new BigDecimal(str); // 取得數值1 BigDecimal one = new BigDecimal("1"); // 原始值除以1,保留scale位小數,進行四舍五入 return b.divide(one, scale, BigDecimal.ROUND_DOWN).toString(); }catch (Exception e){ e.printStackTrace(); } return str; } }