我想计算以BigDecimal格式接收的数字的大小:
BigDecimal number1 = new BigDecimal("17"); int percentage = 50; BigDecimal percentageAmount = number1.multiply(new BigDecimal(percentage/100));
但我得了0分!
将分割结果转换为 double
double
BigDecimal percentageAmount = number1.multiply(new BigDecimal((double)percentage/100));
或者,将值设为100到100.0。
BigDecimal percentageAmount = number1.multiply(new BigDecimal(percentage/100.0));
最佳方法
BigDecimal percentageAmount = number1.multiply(BigDecimal.valueOf((double)percentage/100));