Results 1 to 4 of 4

Thread: help with math statement...

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    8

    help with math statement...

    new to C#... trying to build a compound interest calculation.
    the error i get is in the for loop on the line:

    amount = principal * (1 + rate) ^ year;

    "Operator "*" cannot be applied to operands of type "decimal" and "double"

    also i noticed in this line, the ^ .(in vb the ^ is for exponents),
    i want to know if there is an equivalent in C#.

    can anyone guide me to the light?


    Code:
    double rate;
    int year;
    string output;
    decimal amount, principal;
    
    principal = System.Convert.ToDecimal(txtDeposit.Text);
    rate = System.Convert.ToDouble(txtInterest.Text);
    
    output = "Year     Amount on Deposit"
    
    for(int iX = 1; iX < 11; iX++)
    {
        amount = principal * (1 + rate) ^ year;
        ouput += iX + "     " + System.String.Format("{0:C}", amount) + "\n";
    }
    
    MessageBox.Show(output, "Compound Interest");


    thanks in advance

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I haven't worked with decimals, but you probably need to explicitly cast the double into a decimal:

    amount = principal * (1 + (decimal)rate) ^ year;

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    8

    tried that too....

    but then the error i get is then

    "Operator "^" cannot be applied to operands of type "double" and "double"

    between the "(1 + (decimal)rate) ^ year;" part


    i think it may have something to do with that ^ operator.... not sure though...

    still trying to find out which operator is for exponentials....

    thanks..

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    ^ is the exclusive or operator (xor); there is no operator for powers. You can use the Math.Pow() function instead.
    Last edited by sunburnt; May 8th, 2003 at 05:16 PM.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width