locked
How to increment int by one value RRS feed

  • Question

  • User1632102518 posted

    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...?

    Tuesday, February 3, 2015 12:23 PM

Answers

  • User2103319870 posted

    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;
    	}
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 3, 2015 12:52 PM
  • User281315223 posted

    @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;
    }

    which would just be called as :

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

    You can see an example of this here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 3, 2015 3:21 PM

All replies

  • User281315223 posted

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

    yourInteger++;
    yourInteger = yourInteger + 1;

    Tuesday, February 3, 2015 12:29 PM
  • User1632102518 posted

    @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.

    Tuesday, February 3, 2015 12:38 PM
  • User2103319870 posted

    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;
    	}
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 3, 2015 12:52 PM
  • User1632102518 posted

    @A2H , Thanks a lot.

    Tuesday, February 3, 2015 1:39 PM
  • User281315223 posted

    @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;
    }

    which would just be called as :

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

    You can see an example of this here.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 3, 2015 3:21 PM