Sean
Sean

Reputation: 61

temperature string to int c#

I want to be able to use 2 progress bars as temp gauges for a winform I am making. the temps are for the cpu & gpu of my PS3. I have a label at the moment for each (CTemp & GTemp) which shows the temp as 70 C (for example)

what I want to do is to get that 70 from the string to an int so that I can use it as a progressbar1.Value I have tried the simple things ( I am a bit of a noob) like int.Parse or int cputemp = convert.toint32(CTemp.Text);

Obviously it is completely wrong, but this is what I have in a button at the moment

string cputemp = PS3.CCAPI.GetTemperatureCELL();
string gputemp = PS3.CCAPI.GetTemperatureRSX();
progressBar1.Value = int.Parse(cputemp);
progressBar2.Value = int.Parse(gputemp);

Anyways, any ideas would be great. Thanks

Upvotes: 0

Views: 618

Answers (1)

David
David

Reputation: 219037

You can convert the string to an integer once you've stripped out all non-numeric characters from the string. There are probably lots of ways to do that, this one should do the trick:

string cputemp = PS3.CCAPI.GetTemperatureCELL();
cputemp = Regex.Replace(cputemp, "[^0-9]", string.Empty);

progressBar1.Value = int.Parse(cputemp);

You might also use TryParse in case something goes wrong, so you can handle the error. Something like this:

int cputempvalue = 0;
if (int.TryParse(cputemp, out cputempvalue))
    progressBar1.Value = cputempvalue;
else
    // integer parsing failed, handle the error here

Upvotes: 2

Related Questions