1

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);

    }
}

Input data Face Value - £1000 Years to Maturity - 20 Rate of Interest - 5

Given the formula: F / (1 + r)^t

why do i get 2.73511... I expect 376.89

spotpush
  • 11
  • 1
  • 4

1 Answers1

0

Divide the percentage by 100 (assuming the users are providing "5" meaning "5%"). And use doubles not ints to avoid truncation and loss of precision during the calculation.

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));

Also, take a look at BigDecimal - for example here. This is probably more appropriate for your situation (handling money values).

andrewJames
  • 19,570
  • 8
  • 19
  • 51
  • Thanks, i was messing around for ages and was getting nowhere. Never would of thought to add this (returnOnInvestment / 100f) to the Math.pow method... – spotpush Feb 29 '20 at 18:42