Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Mortgage Calculator Error

Mortgage Calculator Error

Scheduled Pinned Locked Moved C#
help
11 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Xonel

    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
    
    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #2

    :doh: textbox.Text ?

    X 1 Reply Last reply
    0
    • P PIEBALDconsult

      :doh: textbox.Text ?

      X Offline
      X Offline
      Xonel
      wrote on last edited by
      #3

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

      Z 1 Reply Last reply
      0
      • X Xonel

        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
        
        J Offline
        J Offline
        JF2015
        wrote on last edited by
        #4

        txtPayment.Text = payment.ToString();

        or:

        txtPayment.Text = payment.ToString("0.00 $");

        X 1 Reply Last reply
        0
        • X Xonel

          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
          
          V Offline
          V Offline
          V 0
          wrote on last edited by
          #5

          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 term

          What is this code for? Own project, a book, homework?

          V.

          D P B 3 Replies Last reply
          0
          • J JF2015

            txtPayment.Text = payment.ToString();

            or:

            txtPayment.Text = payment.ToString("0.00 $");

            X Offline
            X Offline
            Xonel
            wrote on last edited by
            #6

            :) Can't believe it was that simple.... Thanks a lot! It worked.. :)

            1 Reply Last reply
            0
            • X Xonel

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

              Z Offline
              Z Offline
              ZurdoDev
              wrote on last edited by
              #7

              Don't convert it to int when trying to put it into a textbox.

              There are only 10 types of people in the world, those who understand binary and those who don't.

              1 Reply Last reply
              0
              • V V 0

                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 term

                What is this code for? Own project, a book, homework?

                V.

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #8

                Very minor, but...

                V. wrote:

                make sure payment is not null

                payment is a double (not double?) 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)

                V 1 Reply Last reply
                0
                • V V 0

                  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 term

                  What is this code for? Own project, a book, homework?

                  V.

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #9

                  V. wrote:

                  and is preferable

                  No it isn't.

                  V. wrote:

                  the best method is

                  Not when a cast will do.

                  1 Reply Last reply
                  0
                  • D DaveyM69

                    Very minor, but...

                    V. wrote:

                    make sure payment is not null

                    payment is a double (not double?) 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)

                    V Offline
                    V Offline
                    V 0
                    wrote on last edited by
                    #10

                    You're absolutely right.

                    V.

                    1 Reply Last reply
                    0
                    • V V 0

                      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 term

                      What is this code for? Own project, a book, homework?

                      V.

                      B Offline
                      B Offline
                      BobJanova
                      wrote on last edited by
                      #11

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups