import java.util.Scanner;

public class zeroCouponBond
{
    public static void main(String[] args)
    {
        Scanner usrObj = new Scanner(System.in);
        System.out.print("Face Value of Bond: ");
        int faceValue = usrObj.nextInt();
        System.out.print("Years To Maturity: ");
        int yearsToMaturity = usrObj.nextInt();
        System.out.print("Rate of Interest: ");
        int returnOnInvestment = usrObj.nextInt();

        double BP1 = (faceValue/(Math.pow((returnOnInvestment + 1), yearsToMaturity)));

        System.out.println("Present Bond Value: "+BP1);

    }
}



输入数据
面值-£1000
到期年限-20
利率-5

给定公式:F /(1 + r)^ t

为什么我得到2.73511 ...
我期望376.89

最佳答案

将百分比除以100(假设用户提供的“ 5”表示“ 5%”)。并使用doubles而不ints以避免在计算过程中被截断和精度损失。

double faceValue = 1_000; //usrObj.nextInt();
double yearsToMaturity = 20; //usrObj.nextInt();
double returnOnInvestment = 5; //usrObj.nextInt();
double BP1 = faceValue / (Math.pow(((returnOnInvestment / 100f) + 1), yearsToMaturity));


另外,请查看BigDecimal-例如here。这可能更适合您的情况(处理货币值)。

08-19 07:59