simple math

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hey all,
given:
String value;

how do you subtract 1 from value assuming it's a number?

it's one of those days...

thanks,
rodchar
 
rodchar said:
hey all,
given:
String value;

how do you subtract 1 from value assuming it's a number?

it's one of those days...

int newval = int.Parse(value) - 1;

If you want to keep it as a string:

value = (int.Parse(value) - 1).ToString();

Chris.
 
rodchar said:
hey all,
given:
String value;

how do you subtract 1 from value assuming it's a number?

it's one of those days...

You parse it (e.g. int.Parse) and then subtract one. Call ToString() on
the result if you want it back as a string.
 
rodchar said:
hey all,
given:
String value;

how do you subtract 1 from value assuming it's a number?

TryParse it to an int, subtract 1, and ToString it back again.

/Søren
 
Hi,

You cannot do it directly as a string (well you could really).
So first you use Convert.ToInt , do the operation and call to string again:

string number = "123"
number = (Convert.ToInt32( number) + 1 ).ToString();
 
rodchar said:
hey all,
given:
String value;

how do you subtract 1 from value assuming it's a number?

it's one of those days...

thanks,
rodchar

If the value is a string ....

int i = Integer.parse(string);
i--;
string = i.ToString();


Regards j1mb0jay
 
Thanks everyone for showing the many different way to do this.
Rod.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top