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;
//Add the value after converting your string value to integer
var AddedResult = Convert.ToInt32(inputnumber)+1;
//Add the leading zero to your result because integer dont hold leading zero
var result = AddedResult.ToString().PadLeft(inputlength, '0');
//Return the result
return result;
}
@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;
// Safely parse your value
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;
}
Member
45 Points
144 Posts
How to increment int by one value
Feb 03, 2015 12:23 PM|sreeja1234|LINK
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...?
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 :
Member
45 Points
144 Posts
Re: How to increment int by one value
Feb 03, 2015 12:38 PM|sreeja1234|LINK
@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.
All-Star
50831 Points
9895 Posts
Re: How to increment int by one value
Feb 03, 2015 12:52 PM|A2H|LINK
AFAIK you have to change the datatype from integer to string since integer dont have leading zero
Sample code with string datatype
Aje
My Blog | Dotnet Funda
Member
45 Points
144 Posts
Re: How to increment int by one value
Feb 03, 2015 01:39 PM|sreeja1234|LINK
@A2H , Thanks a lot.
All-Star
114593 Points
18503 Posts
MVP
Re: How to increment int by one value
Feb 03, 2015 03:21 PM|Rion Williams|LINK
It's just because the values need to be formatted using a leading zero :
which would just be called as :
You can see an example of this here.