帮助计算然后显示百分比

编程入门 行业动态 更新时间:2024-10-27 03:38:33
本文介绍了帮助计算然后显示百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在为我玩的游戏开发技能计算器。目前我遇到了显示百分比的正确结果的问题。我环顾四周,尝试在不同的地方发布不同的解决方案,但仍然有一些问题。 这是我到目前为止:

private void CalcBtn_Click( object sender,EventArgs e) { // 变量 int currentXp; // 当前技能经验 int 目标; // 体验目标 int remainingXp; int 百分比; currentXp = int .Parse(CurrentText.Text); // 从文本框中获取当前exp goal = INT .Parse(GoalText.Text); // 获取我的目标 百分比=( 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 ) { // 通过显示错误消息或其他内容来管理错误! // ... } // double currentXp = 0 。 0 ; ok = double .TryParse(CurrentText.Text.Trim(), out currrentXp); if (!ok || currentXp < 0 ) { // 管理上面的错误数据! // ... } // double percentage = 100 .0 * currentXp / goal;

4.最后你可以在UI中显示多少小数想要或没有小数。例如,为了显示带有2位小数的百分比,代码为:

PercentageLabel.Text = string 。格式( {0:#,0.00},百分比);

嗯。 如果我尝试你的代码:

int currentXp; // 当前技能经验 int 目标; // 体验目标 int 百分比; currentXp = 48500000 ; // 从文本框中获取当前exp goal = 50000000 ; // 获取我的目标 百分比=( 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 use TryParse and not Parse. 2.Then you should use double and not int 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);

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.

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.

更多推荐

帮助计算然后显示百分比

本文发布于:2023-11-22 09:53:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616961.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:百分比

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!