|
-
May 8th, 2003, 01:26 PM
#1
Thread Starter
New Member
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
-
May 8th, 2003, 01:30 PM
#2
PowerPoster
I haven't worked with decimals, but you probably need to explicitly cast the double into a decimal:
amount = principal * (1 + (decimal)rate) ^ year;
-
May 8th, 2003, 01:35 PM
#3
Thread Starter
New Member
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..
-
May 8th, 2003, 05:10 PM
#4
^ 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|