简体   繁体   English

如何在Java中执行贷款方程式?

[英]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. 对于第一个方程式,我首先一直遇到错误,因为它希望答案为double,并一直告诉我,对于我的费率变量,我无法从double转换为float,因此必须进行强制转换。 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 . 您缺少一些强制类型转换, log应该是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: 当我将所有内容都转换为double时(为什么您如此坚持使用float?使用double意味着您无需将这些强制转换放在任何地方),我得到以下输出:

Infinity 无穷
52.23012630649508 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 5.659799424266714E-133

Floating point arithmetic will result in your denominator being zero, since that value +1 will lose precision, thus: 浮点运算将导致分母为零,因为该值+1会失去精度,因此:

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

Yields 0.0 and your denominator will cause the second result to be infinity. 屈服值为0.0 ,分母将导致第二个结果为无穷大。

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 您不应该将float / double或raw整数用于货币值,而应使用BigDecimal: 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. 这使得不必执行强制的double-> float强制转换,我希望错误消失。 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 . 唯一可能的零除是log(1+(rate/1200))等于0 This is possible if (rate/1200) returns 0 . 如果(rate/1200)返回0则有可能。 You might solve this by casting (rate/1200) to a float for both occurences. 您可以通过将两种情况都转换为(rate/1200)浮点数来解决此问题。

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. 我认为错误在于rate/1200始终为0因为1200是整数,除法的解也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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