Click here to Skip to main content
15,915,867 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I've the following code:-

C#
int p, t, n = 1;
               decimal r;

               p = Convert.ToInt32(txtLAmount.Text);
               r = Convert.ToDecimal(lblroibuss.Text);
               t = Convert.ToInt32(txtLyear.SelectedItem.Value);

               if (lblprocfees.Text == "Annually")
               { n = 1; }
               else if (lblprocfees.Text == "Quarterly")
               { n = 4; }
               else if (lblprocfees.Text == "Monthly")
               {
                   n = 12;
               }

               n = n * t;

               double R = Convert.ToDouble(1 + (r / 400));
               double Ramt1 = Convert.ToDouble( p * (Math.Pow(R, n) - 1));
               double Ramt2 = Convert.ToDouble(1 - Math.Pow(R, -1 / 3));
               double amt = Ramt1 / Ramt2;
               decimal a = Convert.ToDecimal( Math.Round(10000 * amt) / 10000);



But when I compile the Ramt1 and 2 are getting 0 values due to which the calculation is giong wrong. Please help if you can...
Posted
Comments
[no name] 5-Aug-15 12:25pm    
Debug your code and that will tell you what went wrong. Stop using the Convert class to do these conversions. You are asking for trouble. Use int.TryParse and variants.
Sergey Alexandrovich Kryukov 5-Aug-15 12:32pm    
This code is full of hard-coded immediate constants, such as 400, 10000, "Annually", etc. The worst thing is comparing with those strings, "magic words" (use, say, enum instead). This is not really maintainable. I would suggest your first re-write code in a neat way.
—SA

When you write 1 / 3, it is an integer division and this the result is 0.0. To make floatting point division, at least one of the operand must be a floatting point.

Also, you should not mix decimal and double without reason. Less conversion is better.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Aug-15 12:48pm    
Good catch, a 5.
—SA
Deepak Kanswal Sharma 5-Aug-15 21:54pm    
you mean I should change the 1/3 to 0.33 ?
Philippe Mori 5-Aug-15 22:40pm    
Or 1.0 / 3.0
Deepak Kanswal Sharma 5-Aug-15 21:55pm    
The conversion is needed as I want to the math.pow and math.round operations is there any other way to do it?
Philippe Mori 5-Aug-15 22:43pm    
There is no reason that r is a decimal and not a double to start with as. It only cause superfluous conversions
Start by using the debugger.
Put a breakpoint on the line
C#
n = n * t;
And look at what the values are.
Step through the code, at each stage working out what you expect to get before each line is executed.
If your get what yopu expect, move on.
If you don't, you can start looking at exactly why you don't.

We can't do this for you, because we have no idea what "sensible values" would be for your inputs, or any clue as to what this is supposed to do. Without that, we would be working blind.

So try it - debugging is a skill which you have to use to develop properly (like all other skills) and it's best to develop it on a small piece of code instead of trying on a huge one!
 
Share this answer
 
Comments
Deepak Kanswal Sharma 5-Aug-15 21:49pm    
I've tried the debug and breakpoint.
Although the values I get are all correct but Ramt2 is getting a value 0.00 which is creating problem in calculation.
just debug your project, you will find your problem
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900