简体   繁体   中英

How to perform loan equations in Java?

I'm trying to make a program that will allow the user to determine different aspects when considering a loan. The first equation is supposed to determine what the monthly payment would be given the loan amount, interest rate, and number of months. The second equation is supposed to determine how many payments(or months) one would have to make given the loan amount, interest rate, and monthly payment. This is the code I'm testing with, but I can't seem to get valid output.

    float amount = 20000;
    float rate = (float) 7.5;
    float months = 60;
    float payment = 450;

    float answer = (float) (((amount*(rate/1200))*(1+(Math.pow((rate/1200), months))))/((1+Math.pow((rate/1200), months))-1));
    System.out.println(answer);

    float answer2 = (log(payment/amount)-log((payment/amount)-(rate/1200)))/(log(1+(rate/1200)));
    System.out.println(answer2);

For the first equation, I first kept getting errors because it wanted the answer to be a double and kept telling me that for my rate variable I couldn't convert from double to float, so I had to put in casts. Once I got the errors to go away, I keep getting infinity for an answer.

For the second equation, it keeps saying I can't divide by zero. Does anybody know any solutions to this. When looking online, it seems as though I'm formulated my equations correctly, so I don't know how to make them work.

Also, if anyone would happen to know a good formula for determining the interest rate, that would be so helpful.

You're missing some casts, and log should be Math.log . When I convert everything to double (why are you so insistent on using floats? Using doubles means you won't need to put those casts everywhere) I get the following output:

Infinity
52.23012630649508

Not sure what the second value is supposed to be, but that seems ok; no warnings and no errors. Regarding the first part, you're dividing by zero. Try this:

System.out.println(Math.pow(rate/1200, months));

And you'll get the following tiny tiny number

5.659799424266714E-133

Floating point arithmetic will result in your denominator being zero, since that value +1 will lose precision, thus:

System.out.println(1+Math.pow(rate/1200, months)-1);

Yields 0.0 and your denominator will cause the second result to be infinity.

You should not use float/double or raw integers for monetary values, use BigDecimal instead: http://download.oracle.com/javase/1.4.2/docs/api/java/math/BigDecimal.html

This makes it unnecessary to do that forced double->float cast and i hope the errors disappear. If not then add a comment with your new code please.

For the second equation:

float answer2 = (log(payment/amount)-log((payment/amount)-(rate/1200)))/(log(1+(rate/1200)));

The only possible division by zero is if log(1+(rate/1200)) equals 0 . This is possible if (rate/1200) returns 0 . You might solve this by casting (rate/1200) to a float for both occurences.

Thus:

float answer2 = (log(payment/amount)-log((payment/amount)-((float)(rate/1200))))/(log(1+((float)(rate/1200))));

I think the error lies in the fact that rate/1200 is always 0 because 1200 is an integer and so is the solution of the division.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM