如何用 Java 计算贷款方程?

发布于 2024-12-05 10:52:53 字数 787 浏览 1 评论 0 原文

我正在尝试制作一个程序,允许用户在考虑贷款时确定不同的方面。第一个方程应该确定在给定贷款金额、利率和月数的情况下每月还款额是多少。第二个方程应该确定在给定贷款金额、利率和每月还款额的情况下,一个人必须支付多少笔付款(或几个月)。这是我正在测试的代码,但我似乎无法获得有效的输出。

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

对于第一个方程,我首先不断收到错误,因为它希望答案是双精度型,并不断告诉我,对于我的速率变量,我无法从双精度型转换为浮点型,所以我必须进行强制转换。一旦错误消失,我就会不断地得到答案。

对于第二个方程,它一直说我不能除以零。有谁知道这个问题的任何解决方案。在网上查看时,似乎我正确地制定了方程式,所以我不知道如何使它们发挥作用。

另外,如果有人碰巧知道确定利率的好公式,那将会非常有帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌上青苔 2024-12-12 10:52:53

您缺少一些转换,log 应该是 Math.log。当我将所有内容都转换为双精度时(为什么你如此坚持使用浮点数?使用双精度意味着你不需要将这些转换放在任何地方)我得到以下输出:

无穷大
52.23012630649508

不确定第二个值应该是什么,但这似乎没问题;没有警告,也没有错误。关于第一部分,您要除以零。试试这个:

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

你会得到以下微小的数字

5.659799424266714E-133

浮点运算将导致分母为零,因为该值 +1 将失去精度,因此:

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

产生 0.0 并且分母将导致第二个结果为无穷大。

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.

去了角落 2024-12-12 10:52:53

您不应使用 float/double 或原始整数作为货币值,而应使用 BigDecimal: http://download.oracle.com/javase/1.4.2/docs/api/java/math/BigDecimal.html

这使得没有必要强制这样做double-> float 强制转换,我希望错误消失。如果没有,请添加您的新代码的评论。

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.

够运 2024-12-12 10:52:53

对于第二个方程:

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

唯一可能被零除的情况是 log(1+(rate/1200)) 等于 0
如果(rate/1200) 返回0,则这是可能的。您可以通过将这两种情况的 (rate/1200) 转换为浮点数来解决此问题。

因此:

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

我认为错误在于 rate/1200 始终为 0 因为 1200 是一个整数,因此分配。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文