在c#中添加数字

发布于 2024-12-03 03:00:03 字数 155 浏览 2 评论 0原文

我有一个数字文本框,我需要将其值添加到另一个数字 我已经尝试过这段代码

String add = (mytextbox.Text + 2)

,但它将数字 2 添加为另一个字符,就像如果我的文本框的值为 13,结果将变为 132

i have a numerical textbox which I need to add it's value to another number
I have tried this code

String add = (mytextbox.Text + 2)

but it add the number two as another character like if the value of my text box is 13 the result will become 132

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

与往事干杯 2024-12-10 03:00:03

mytextbox.Text 的类型是字符串。您需要将其解析为数字才能执行整数算术,例如

int parsed = int.Parse(mytextbox.Text);
int result = parsed + 2;
string add = result.ToString(); // If you really need to...

请注意,您可能希望使用 < code>int.TryParse 以便处理文本框内容不是整数的情况,而不必捕获异常。例如:

int parsed;
if (int.TryParse(mytextbox.Text, out parsed))
{
    int result = parsed + 2;
    string add = result.ToString();
    // Use add here    
}
else
{
    // Indicate failure to the user; prompt them to enter an integer.
}

The type of mytextbox.Text is string. You need to parse it as a number in order to perform integer arithmetic, e.g.

int parsed = int.Parse(mytextbox.Text);
int result = parsed + 2;
string add = result.ToString(); // If you really need to...

Note that you may wish to use int.TryParse in order to handle the situation where the contents of the text box is not an integer, without having to catch an exception. For example:

int parsed;
if (int.TryParse(mytextbox.Text, out parsed))
{
    int result = parsed + 2;
    string add = result.ToString();
    // Use add here    
}
else
{
    // Indicate failure to the user; prompt them to enter an integer.
}
向日葵 2024-12-10 03:00:03
String add = (Convert.ToInt32(mytextbox.Text) + 2).ToString();

您需要将文本转换为整数才能进行计算。

String add = (Convert.ToInt32(mytextbox.Text) + 2).ToString();

You need to convert the text to an integer to do the calculation.

梦醒时光 2024-12-10 03:00:03
const int addend = 2; 
string myTextBoxText = mytextbox.Text;
var doubleArray = new double[myTextBoxText.ToCharArray().Length];
for (int index = 0; index < myTextBoxText.ToCharArray().Length; index++)
{
    doubleArray[index] = 
        Char.GetNumericValue(myTextBoxText.ToCharArray()[index]) 
        * (Math.Pow(10, (myTextBoxText.ToCharArray().Length - 1) - index));
}
string add  = 
    (doubleArray.Aggregate((term1, term2) => term1 + term2) + addend).ToString();
const int addend = 2; 
string myTextBoxText = mytextbox.Text;
var doubleArray = new double[myTextBoxText.ToCharArray().Length];
for (int index = 0; index < myTextBoxText.ToCharArray().Length; index++)
{
    doubleArray[index] = 
        Char.GetNumericValue(myTextBoxText.ToCharArray()[index]) 
        * (Math.Pow(10, (myTextBoxText.ToCharArray().Length - 1) - index));
}
string add  = 
    (doubleArray.Aggregate((term1, term2) => term1 + term2) + addend).ToString();
独守阴晴ぅ圆缺 2024-12-10 03:00:03
string add=(int.Parse(mytextbox.Text) + 2).ToString()

如果你想确保转换不会引发任何异常

  int textValue = 0;
  int.TryParse(TextBox.text, out textValue);
  String add = (textValue + 2).ToString();
string add=(int.Parse(mytextbox.Text) + 2).ToString()

if you want to make sure the conversion doesn't throw any exception

  int textValue = 0;
  int.TryParse(TextBox.text, out textValue);
  String add = (textValue + 2).ToString();
迟到的我 2024-12-10 03:00:03
int intValue = 0;
if(int.TryParse(mytextbox.Text, out intValue))
{
    String add = (intValue + 2).ToString();
}

我更喜欢 TryPase,那么你知道回退将为零(或者你定义为 intValue 的默认值的任何内容)

int intValue = 0;
if(int.TryParse(mytextbox.Text, out intValue))
{
    String add = (intValue + 2).ToString();
}

I prefer TryPase, then you know the fallback is going to be zero (or whatever you have defined as the default for intValue)

黒涩兲箜 2024-12-10 03:00:03

您可以使用 int.Parse 方法将文本内容解析为整数:

String add = (int.Parse(mytextbox.Text) + 2).ToString();

You can use the int.Parse method to parse the text content into an integer:

String add = (int.Parse(mytextbox.Text) + 2).ToString();
听风念你 2024-12-10 03:00:03

其他人已经发布了最常见的答案,但只是为了给您提供替代方案,您可以使用属性来检索 TextBox 的整数值。

如果您需要多次重复使用整数,这可能是一个好方法:

private int MyTextBoxInt
{
    get
    {
        return Int32.Parse(mytextbox.Text);
    }
}

然后您可以像这样使用该属性:

int result = this.MyTextBoxInt + 2;

Others have posted the most common answers, but just to give you an alternative, you could use a property to retrieve the integer value of the TextBox.

This might be a good approach if you need to reuse the integer several times:

private int MyTextBoxInt
{
    get
    {
        return Int32.Parse(mytextbox.Text);
    }
}

And then you can use the property like this:

int result = this.MyTextBoxInt + 2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文