BigDecimal類中提供了兩個方法來取余數(shù):remainder()和remainder(BigDecimal divisor)。
BigDecimal num = new BigDecimal("10.5");
BigDecimal remainder = num.remainder(BigDecimal.ONE);
System.out.println("余數(shù):" + remainder); // 輸出:0.5
BigDecimal num1 = new BigDecimal("10.5");
BigDecimal num2 = new BigDecimal("3");
BigDecimal remainder = num1.remainder(num2);
System.out.println("余數(shù):" + remainder); // 輸出:1.5