Doesn't Matter
Doesn't Matter

Reputation: 405

Math.pow difficulty

When I enter 1000 for investment amount 4.25 for monthly interest rate and 1 for years, why do I get the result 4.384414858452464E11 instead of the expected 1043.34?

import java.util.Scanner;

public class FinancialApplicationFutureInvestment_13 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter investment amount: ");
        int investmentAmount = input.nextInt();
        System.out.println("Enter monthly interest rate: ");
        double monthlyInterestRate = input.nextDouble();
        System.out.println("Enter number of years");
        int numOfYears = input.nextInt();

        double futureInvestmentValue = investmentAmount *
            (Math.pow(1 + monthlyInterestRate, numOfYears * 12));
        System.out.println("Accumulated value is: " + futureInvestmentValue);

        double test = Math.pow(1 + monthlyInterestRate, numOfYears * 12);
        System.out.println(test);
    }
}

Upvotes: 1

Views: 931

Answers (5)

sciecore
sciecore

Reputation: 11

you should use casting in System.out.println (double to int) -- (int)(futureInvestmentValue * 100) / 100.0

Upvotes: 1

Pierre Gardin
Pierre Gardin

Reputation: 684

well, you compute 1+4.25 (5.25) as monthly interest rate, instead of 1+(4.25/100) .

Upvotes: 2

dku.rajkumar
dku.rajkumar

Reputation: 18568

the formula is

A = P(1+r/100)^n

so it should be

investmentAmount * (Math.pow(1 + (monthlyInterestRate/100), numOfYears * 12));

Upvotes: 2

IvoTops
IvoTops

Reputation: 3531

Monthly interest rate will probably need to be entered as 0.0425

Upvotes: 5

Potatoswatter
Potatoswatter

Reputation: 137810

1 + monthlyInterestRate

Is monthlyInterestRate a raw factor, or is it expressed in percentage points?

Try dividing by one hundred.

Upvotes: 2

Related Questions