Mortgage Calculator Error
-
Hi, I have this error in my code that am having difficulties in rectifying it. Initially, my code looked like this..
private void CalculateTheMortgage()
{
// (Loan Value) * (1 + r/12) ^ p = (12x / r) * ((1 + r/12)^p - 1)
// payment = (((Loan Value) * (1 + r/12) ^ p) * r)/ (12 * ((1 + r/12)^p - 1)));
double loanAmount = (double)txtLoanAmount.CurrentValue; // price of total mortgage before down payment
double taxesPerYear = (double)txtPropertyTax.CurrentValue; // this will divided by 12 and added to the monthly payment
double downPayment = (double)txtDownPayment.CurrentValue; // down payment will be subtracted from the loan
double interestRate = (double)udInterest.Value / 100; // calculate interest from 100%
double termOfLoan = (double)(udTerm.Value * 12); // monthly term
double propertyTax = (double)txtPropertyTax.CurrentValue;
double insurance = (double)txtInsurance.CurrentValue;double payment = (loanAmount - downPayment) \* (Math.Pow((1 + interestRate/12), termOfLoan) \* interestRate)/(12 \* (Math.Pow((1+interestRate/12), termOfLoan) - 1)); // add on a monthly property tax payment = payment + (propertyTax + insurance) / 12; txtPayment.CurrentValue = (int)payment; }
and i changed it a little to this...
private void CalculateTheMortgage()
{
// (Loan Value) * (1 + r/12) ^ p = (12x / r) * ((1 + r/12)^p - 1)
// payment = (((Loan Value) * (1 + r/12) ^ p) * r)/ (12 * ((1 + r/12)^p - 1)));
double loanAmount = Convert.ToDouble(txtLoanAmount.Text);// price of total mortgage before down payment
double taxesPerYear = Convert.ToDouble(txtPropertyTax.Text); // this will divided by 12 and added to the monthly payment
double downPayment = Convert.ToDouble(txtDownPayment.Text); // down payment will be subtracted from the loan
double interestRate = (double)udInterest.Value / 100; // calculate interest from 100%
double termOfLoan = (double)(udTerm.Value * 12); // monthly term
double propertyTax = Convert.ToDouble(txtPropertyTax.Text);
double insurance = Convert.ToDouble(txtInsurance.Text);double payment = (loanAmount - downPayment) \* (Math.Pow((1 + interestRate / 12), termOfLoan) \* interestRate) / (12 \* (Math.Pow((1 + inter
:doh: textbox.Text ?
-
:doh: textbox.Text ?
-
Hi, I have this error in my code that am having difficulties in rectifying it. Initially, my code looked like this..
private void CalculateTheMortgage()
{
// (Loan Value) * (1 + r/12) ^ p = (12x / r) * ((1 + r/12)^p - 1)
// payment = (((Loan Value) * (1 + r/12) ^ p) * r)/ (12 * ((1 + r/12)^p - 1)));
double loanAmount = (double)txtLoanAmount.CurrentValue; // price of total mortgage before down payment
double taxesPerYear = (double)txtPropertyTax.CurrentValue; // this will divided by 12 and added to the monthly payment
double downPayment = (double)txtDownPayment.CurrentValue; // down payment will be subtracted from the loan
double interestRate = (double)udInterest.Value / 100; // calculate interest from 100%
double termOfLoan = (double)(udTerm.Value * 12); // monthly term
double propertyTax = (double)txtPropertyTax.CurrentValue;
double insurance = (double)txtInsurance.CurrentValue;double payment = (loanAmount - downPayment) \* (Math.Pow((1 + interestRate/12), termOfLoan) \* interestRate)/(12 \* (Math.Pow((1+interestRate/12), termOfLoan) - 1)); // add on a monthly property tax payment = payment + (propertyTax + insurance) / 12; txtPayment.CurrentValue = (int)payment; }
and i changed it a little to this...
private void CalculateTheMortgage()
{
// (Loan Value) * (1 + r/12) ^ p = (12x / r) * ((1 + r/12)^p - 1)
// payment = (((Loan Value) * (1 + r/12) ^ p) * r)/ (12 * ((1 + r/12)^p - 1)));
double loanAmount = Convert.ToDouble(txtLoanAmount.Text);// price of total mortgage before down payment
double taxesPerYear = Convert.ToDouble(txtPropertyTax.Text); // this will divided by 12 and added to the monthly payment
double downPayment = Convert.ToDouble(txtDownPayment.Text); // down payment will be subtracted from the loan
double interestRate = (double)udInterest.Value / 100; // calculate interest from 100%
double termOfLoan = (double)(udTerm.Value * 12); // monthly term
double propertyTax = Convert.ToDouble(txtPropertyTax.Text);
double insurance = Convert.ToDouble(txtInsurance.Text);double payment = (loanAmount - downPayment) \* (Math.Pow((1 + interestRate / 12), termOfLoan) \* interestRate) / (12 \* (Math.Pow((1 + inter
-
Hi, I have this error in my code that am having difficulties in rectifying it. Initially, my code looked like this..
private void CalculateTheMortgage()
{
// (Loan Value) * (1 + r/12) ^ p = (12x / r) * ((1 + r/12)^p - 1)
// payment = (((Loan Value) * (1 + r/12) ^ p) * r)/ (12 * ((1 + r/12)^p - 1)));
double loanAmount = (double)txtLoanAmount.CurrentValue; // price of total mortgage before down payment
double taxesPerYear = (double)txtPropertyTax.CurrentValue; // this will divided by 12 and added to the monthly payment
double downPayment = (double)txtDownPayment.CurrentValue; // down payment will be subtracted from the loan
double interestRate = (double)udInterest.Value / 100; // calculate interest from 100%
double termOfLoan = (double)(udTerm.Value * 12); // monthly term
double propertyTax = (double)txtPropertyTax.CurrentValue;
double insurance = (double)txtInsurance.CurrentValue;double payment = (loanAmount - downPayment) \* (Math.Pow((1 + interestRate/12), termOfLoan) \* interestRate)/(12 \* (Math.Pow((1+interestRate/12), termOfLoan) - 1)); // add on a monthly property tax payment = payment + (propertyTax + insurance) / 12; txtPayment.CurrentValue = (int)payment; }
and i changed it a little to this...
private void CalculateTheMortgage()
{
// (Loan Value) * (1 + r/12) ^ p = (12x / r) * ((1 + r/12)^p - 1)
// payment = (((Loan Value) * (1 + r/12) ^ p) * r)/ (12 * ((1 + r/12)^p - 1)));
double loanAmount = Convert.ToDouble(txtLoanAmount.Text);// price of total mortgage before down payment
double taxesPerYear = Convert.ToDouble(txtPropertyTax.Text); // this will divided by 12 and added to the monthly payment
double downPayment = Convert.ToDouble(txtDownPayment.Text); // down payment will be subtracted from the loan
double interestRate = (double)udInterest.Value / 100; // calculate interest from 100%
double termOfLoan = (double)(udTerm.Value * 12); // monthly term
double propertyTax = Convert.ToDouble(txtPropertyTax.Text);
double insurance = Convert.ToDouble(txtInsurance.Text);double payment = (loanAmount - downPayment) \* (Math.Pow((1 + interestRate / 12), termOfLoan) \* interestRate) / (12 \* (Math.Pow((1 + inter
There are many things wrong here.
Convert.ToDouble
will work and is preferable of
(double)somevalue
but the best method is
double.TryParse or int.TryParse
Secondly you need to use
txtPayment.Text = payment.ToString();
and make sure payment is not null. If you do want an integer, you need to indeed first cast the double to an integer and then that integer to a string (or use round and floor functions) Why do you cast to double in these cases?
double interestRate = (double)udInterest.Value / 100; // calculate interest from 100%
double termOfLoan = (double)(udTerm.Value * 12); // monthly termWhat is this code for? Own project, a book, homework?
V.
-
When i write it like this
txtPayment.Text = (int)payment;
am getting this error
Cannot implicitly convert type 'int' to 'string'
with a blue line under '(int)'
-
There are many things wrong here.
Convert.ToDouble
will work and is preferable of
(double)somevalue
but the best method is
double.TryParse or int.TryParse
Secondly you need to use
txtPayment.Text = payment.ToString();
and make sure payment is not null. If you do want an integer, you need to indeed first cast the double to an integer and then that integer to a string (or use round and floor functions) Why do you cast to double in these cases?
double interestRate = (double)udInterest.Value / 100; // calculate interest from 100%
double termOfLoan = (double)(udTerm.Value * 12); // monthly termWhat is this code for? Own project, a book, homework?
V.
Very minor, but...
V. wrote:
make sure payment is not null
payment
is adouble
(notdouble?
) so cannot be null ;)Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
There are many things wrong here.
Convert.ToDouble
will work and is preferable of
(double)somevalue
but the best method is
double.TryParse or int.TryParse
Secondly you need to use
txtPayment.Text = payment.ToString();
and make sure payment is not null. If you do want an integer, you need to indeed first cast the double to an integer and then that integer to a string (or use round and floor functions) Why do you cast to double in these cases?
double interestRate = (double)udInterest.Value / 100; // calculate interest from 100%
double termOfLoan = (double)(udTerm.Value * 12); // monthly termWhat is this code for? Own project, a book, homework?
V.
V. wrote:
and is preferable
No it isn't.
V. wrote:
the best method is
Not when a cast will do.
-
Very minor, but...
V. wrote:
make sure payment is not null
payment
is adouble
(notdouble?
) so cannot be null ;)Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
There are many things wrong here.
Convert.ToDouble
will work and is preferable of
(double)somevalue
but the best method is
double.TryParse or int.TryParse
Secondly you need to use
txtPayment.Text = payment.ToString();
and make sure payment is not null. If you do want an integer, you need to indeed first cast the double to an integer and then that integer to a string (or use round and floor functions) Why do you cast to double in these cases?
double interestRate = (double)udInterest.Value / 100; // calculate interest from 100%
double termOfLoan = (double)(udTerm.Value * 12); // monthly termWhat is this code for? Own project, a book, homework?
V.
double.Parse is not equivalent to a double cast. One does a string-to-number conversion, the other does a number-to-number conversion. You can use Convert.ToDouble to do either, but I disagree that it's preferable for either scenario: use the mechanism that makes sense in context. A cast translates to a single IL opcode, not a whole method call, after all.