如何找到百分比符号 [英] How to find the percentage symbol

查看:147
本文介绍了如何找到百分比符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨frds,

我在windows窗体中工作。我想做百分比计算。因此,如果百分比符号为百分比表示它将执行百分比操作,否则它将显示消息框。我怎么能这样做。



我尝试过:



这是用于百分比计算。但是这只想继续只有百分比符号才能继续

十进制PercentageRate = Convert.ToDecimal(this.textBox1.Text);

int Rate = Convert.ToInt32(textBox2.Text .Replace(%,));

int Total = Convert.ToInt32(this.textBox2.Text.Replace(%,))* Convert.ToInt32(PercentageRate) / 100 + Convert.ToInt32(this.textBox1.Text); this.textBox3.Text = Total.ToString();

Hi frds,
I am working in windows form. I want to do percentage calculation. So if Percentage symbol is percent means it will do percentage operation otherwise it will show message box. How can i do this.

What I have tried:

this is for percentage calculation. But this want to proceed only percentage symbol is persent
decimal PercentageRate = Convert.ToDecimal(this.textBox1.Text);
int Rate = Convert.ToInt32(textBox2.Text.Replace("%", ""));
int Total = Convert.ToInt32(this.textBox2.Text.Replace("%", "")) * Convert.ToInt32(PercentageRate) / 100 + Convert.ToInt32(this.textBox1.Text); this.textBox3.Text = Total.ToString();

推荐答案

这很容易检查:

It's pretty easy to check:
string s = "25%";
if (s.Contains("%"))
   {
   // It's a percentage



或者:


Alternatively:

string s = "25%";
...
s = s.Trim();
if (s.EndsWith("%"))
   {
   // It's a percentage



但......代码有点......差。

不要使用Convert.To - 使用Int.TryParse或Double。改为尝试TryParse。

他们给你一个bool结果,告诉你这不是一个有效的整数,或者这不是一个有效的双重,并允许你告诉用户有一个问题,而不是继续可能错误的值,用户不知道它。


But...That codes a bit...poor.
Don't use Convert.To - use Int.TryParse or Double.TryParse instead.
They give you a bool result which tells you "this wasn't a valid integer", or "this wasn't a valid double" and allows you to tell the user there is a problem instead of continuing with possibly the wrong values and the user doesn't know it.


这篇关于如何找到百分比符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆