提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
BigDecimal的复利计算。
import java.math.BigDecimal; import java.text.NumberFormat; public class Main /*时代Java - N o w J a v a . c o m 提 供*/ { public static void main(String args[]) { // initial principal amount before interest BigDecimal principal = BigDecimal.valueOf(1000.0); BigDecimal rate = BigDecimal.valueOf(0.05); // interest rate // display headers System.out.printf("%s%20s%n", "Year", "Amount on deposit"); // calculate amount on deposit for each of ten years for (int year = 1; year <= 10; year++) { // calculate new amount for specified year BigDecimal amount = principal.multiply(rate.add(BigDecimal.ONE).pow(year)); // display the year and the amount