## 5 replies

Last post Feb 03, 2015 03:21 PM by Rion Williams

• sreeja1234

Member

45 Points

144 Posts

### How to increment int by one value

I need to increment my input integer by one value everyitme I make a call to a method .

Input:  00,01,02,03,,,,99

Output: 01,02,03....99

How to implement this logic...?

• Rion William...

All-Star

114593 Points

18503 Posts

MVP

### Re: How to increment int by one value

Feb 03, 2015 12:29 PM|Rion Williams|LINK

If you already have an existing integer, you could simply use either of the following notations :

```yourInteger++;
yourInteger = yourInteger + 1;```

• sreeja1234

Member

45 Points

144 Posts

### Re: How to increment int by one value

@Rion, that logic doesn't return me the exact ouput.

For Suppose if I pass 02 as Input, then it returns 3. But I want 03 as ouput.

• A2H

All-Star

50831 Points

9895 Posts

### Re: How to increment int by one value

#### sreeja1234

For Suppose if I pass 02 as Input, then it returns 3. But I want 03 as ouput.

AFAIK you have to change the datatype from integer to string since integer dont have leading zero

Sample code with string datatype

```public string AddValue(string inputnumber)
{
//Get the passed input length
int inputlength = inputnumber.ToString().Length;

//Return the result
return result;
}```
Thanks,
Aje
My Blog | Dotnet Funda
• sreeja1234

Member

45 Points

144 Posts

### Re: How to increment int by one value

@A2H , Thanks a lot.

• Rion William...

All-Star

114593 Points

18503 Posts

MVP

### Re: How to increment int by one value

Feb 03, 2015 03:21 PM|Rion Williams|LINK

#### sreeja1234

@Rion, that logic doesn't return me the exact ouput.

For Suppose if I pass 02 as Input, then it returns 3. But I want 03 as ouput.

It's just because the values need to be formatted using a leading zero :

```public static string IncrementWithFormat(string s)
{
// Value to store your integer
int i;

if(Int32.TryParse(s, out i))
{
// Output an incremented version of your value
return (++i).ToString("00");
}
// It wasn't an integer, output null (or throw an error)
return null;
}```

which would just be called as :

`IncrementWithFormat("02"); // yields "03"`

You can see an example of this here.