Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have been working on a skill calculator for a game I play. currently I am having issues with getting the correct result of a percentage to display. I have looked around and tried different solutions posted at different places and am still having some issues.

here is what I have so far:

C#
private void CalcBtn_Click(object sender, EventArgs e)
   {
       //variables
       int currentXp; //current experience in the skill
       int goal; // experience goal
       int remainXp;
       int percentage;

       currentXp = int.Parse(CurrentText.Text); //gets current exp from a text box
       goal = int.Parse(GoalText.Text); //gets what I put in as my goal

       percentage = (int)(((double)currentXp * 100) / (double)goal); // the percentage on the PercentageLabel shows up as
           //say if my current experience is 48500000 and the goal is 50000000 the result should be 97% but it shows up as 9,700.00%
           //if I divide the result by 100 it shows up as 0.00%
           //also have tried ((currentXp / goal) * 100) the result shows up as 0.00% with that as well.
       remainXp = (goal - currentXp);

       RemainingLabel.Text = remainXp.ToString("N");
       PercentageLabel.Text = percentage.ToString("p");

   }
Posted

1.First remark you should do some validation of the user input and also to manage the exception case, like goal that could have 0 value ==> division with 0 will generate exception. Also you should use TryParse and not Parse.

2.Then you should use double and not int in temp computation of the percent value like below:
C#
double goal = 1;
bool ok = double.TryParse(GoalText.Text.Trim(), out goal);
if(!ok || goal <= 0.0)
{
   //Manage the error by showing an error message or something!
   //...
}
//
double currentXp = 0.0;
ok = double.TryParse(CurrentText.Text.Trim(), out currrentXp);
if(!ok || currentXp < 0)
{
  //Manage the error data like above!
  //... 
}
//
double percentage = 100.0*currentXp/goal;

4.Finally you could show in the UI as many decimals you want, or no decimals. For example for showing the percent with 2 decimals here is the code:
C#
PercentageLabel.Text = string.Format("{0:#,0.00}", percentage); 
 
Share this answer
 
v2
Um.
If I try your code:
C#
int currentXp; //current experience in the skill
int goal; // experience goal
int percentage;

currentXp = 48500000; //gets current exp from a text box
goal = 50000000; //gets what I put in as my goal

percentage = (int)(((double)currentXp * 100) / (double)goal);

Then percentage shows up as 97.

So...I'd start by checking you have the right values in your textbox and GoalText - the code works if you feed in your sample values.

Use the debugger to look at exactly what you have.
 
Share this answer
 
First,define your percentage variable as double.

C#
double percentage;


Then,change then line where you are calculating the percentage with this.

C#
percentage = (((double)currentXp) / (double)goal);


The problem was that you were storing the percentage in an int variable.
 
Share this answer
 
its for the..
C#
RemainingLabel.Text = remainXp.ToString("N");
PercentageLabel.Text = percentage.ToString("p");

See this..
You'll understand.
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx[^]
 
Share this answer
 
v2
Ty For the help. I got it. I'm still fairly new to some things the text book I went through for some course I just completed wasn't that helpful when it came to this particular issue.
 
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