帮助计算然后显示百分比 [英] Help with calculating then displaying a percentage
问题描述
我一直在为我玩的游戏开发技能计算器。目前我遇到了显示百分比的正确结果的问题。我环顾四周,尝试在不同的地方发布不同的解决方案,但仍然有一些问题。
这是我到目前为止:
private void CalcBtn_Click(< span class =code-keyword> object sender,EventArgs e)
{
// 变量
int currentXp; // 当前技能经验
int 目标; // 体验目标
int remainingXp;
int 百分比;
currentXp = int .Parse(CurrentText.Text); // 从文本框中获取当前exp
goal = INT 跨度> .Parse(GoalText.Text); // 获取我的目标
百分比=(< span class =code-keyword> int )((( double )currentXp * 100 )/( double )目标); // PercentageLabel上的百分比显示为
// 如果我目前的经验是48500000并且目标是50000000,结果应该是97%,但它显示为9,700.00%
// 如果我将结果除以100则显示为0.00%
// 也尝试了((currentXp / goal)* 100)结果显示为0.00%。
remainingXp =(goal-currentXp);
RemainingLabel.Text = remainingXp.ToString( N);
PercentageLabel.Text = percentage.ToString( p);
}
1.首先,您应该对用户输入进行一些验证并管理例外情况,例如可能有0值的目标==>除0会产生异常。你也应该使用TryParse
而不是Parse
。
2.然后你应该在百分比值的临时计算中使用double
而不是int
,如下所示:
double goal = 1 ;
bool ok = double .TryParse(GoalText.Text.Trim(), out 目标);
if (!ok || goal < = 0 。 0 )
{
// < span class =code-comment>通过显示错误消息或其他内容来管理错误!
// ...
}
//
double currentXp = 0 。 0 跨度>;
ok = double .TryParse(CurrentText.Text.Trim(), out currrentXp);
if (!ok || currentXp < 0 )
{
// 管理上面的错误数据!
// ...
}
< span class =code-comment> //
double percentage = 100 .0 * currentXp / goal;
4.最后你可以在UI中显示多少小数想要或没有小数。例如,为了显示带有2位小数的百分比,代码为:
PercentageLabel.Text = string 。格式( {0:#,0.00},百分比);
嗯。
如果我尝试你的代码:
int currentXp; // 当前技能经验
int 目标; // 体验目标
int 百分比;
currentXp = 48500000 ; // 从文本框中获取当前exp
goal = 50000000 跨度>; // 获取我的目标
百分比=(< span class =code-keyword> int )((( double )currentXp * 100 )/( double )目标);
然后百分比
显示为97.
所以...我首先要检查你的文本框和GoalText中是否有正确的值 - 如果你有代码可以运行提供样本值。
使用调试器查看您的确切内容。
首先,定义百分比变量加倍。
double 百分比;
然后,改变你用这个计算百分比的行。
percentage =((( double )currentXp)/(双)目标);
问题是你将百分比存储在一个int变量中
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:
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");
}
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 useTryParse
and notParse
.
2.Then you should usedouble
and notint
in temp computation of the percent value like below:
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:
PercentageLabel.Text = string.Format("{0:#,0.00}", percentage);
Um.
If I try your code:
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);
Thenpercentage
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.
First,define your percentage variable as double.
double percentage;
Then,change then line where you are calculating the percentage with this.
percentage = (((double)currentXp) / (double)goal);
The problem was that you were storing the percentage in an int variable.
这篇关于帮助计算然后显示百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!