java输出不符合预期的零息债券计算器
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
# 1 楼答案
将百分比除以100(假设用户提供“5”表示“5%”)。并使用
doubles
而不是ints
以避免计算过程中的截断和精度损失另外,看看
BigDecimal
——例如here。这可能更适合您的情况(处理货币价值)