Question

I'm writing a mortgage calculator app. I have initialized doubles purchPrice, downPay, and interestRate. All three doubles are initialized to 0.0. I have three payment options tenPay, twentyPay, and thirtyPay, all initialized to 0.0. After the following lines of code are ran, tenPay becomes NaN, and the other two are 0.0 as expected. I can't figure out why this is happening and would love another set of eyes on it.

tenPay = ((purchPrice - downPay) * (interestRate * .01) / (10/12));
twentyPay = ((purchPrice - downPay) * (interestRate * .01) / (20/12));
thirtyPay = ((purchPrice - downPay) * (interestRate * .01) / (30/12));

I'm pretty sure my math is faulty, please disregard.

Était-ce utile?

La solution

Your problem is with the 10/12 part. Java is interpreting them both as integers, and rounding the result down, so it becomes 0. Dividing by 0 produces NaN.
Also, both the 20/12 and the 30/12 will evaluate to 2. Although this will not produce an error, it will not return the correct result.
You can fix all of these issues by changing every 12 to 12.0.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top