当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to use math operators with strings?

问题描述:

This question already has an answer here:

  • How can I convert String to Int?

    20 answers

网友答案:
int RTPower = Int32.Parse(powerTextBox.Text);

or for decimal values

decimal RTPower = Decimal.Parse(powerTextBox.Text);

You need to convert the value from a string to an int.

Also, I assume you are new to c# - my advice would be to avoid using var and explicitly declare your variables. It will make things clear and easier for you to learn and understand.

网友答案:

You can convert to int like

int x = Int32.Parse(RTPower);

then you can compare x.


If, however, you know that the user input will be between [0, 9] then you could use

if(RTPower >= "0" && <= "1")

because strings are compared lexicographically so "1" is under "9" but "10" is under "2".

The first way is much better though, because it works for all numerical user inputs

分享给朋友:
您可能感兴趣的文章:
随机阅读: