 We are excited to announce that the ASP.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

## 7 replies

Last post Sep 27, 2016 07:26 AM by PaulTheSmith

• ### Replace the last number/digit of a value

I'm trying to iterate and generate values based on certain conditions. My start value is 7, and I am iterating over it 15 times so the output should be 7, 14, 21 etc. But basically I am trying to add 7 to the start value 15 times with one caveat. If the last digit is 9, then I want to replace the 9 with a 6 and then start the iteration from the next value onwards. Such as 49 will become 46, then for the next iteration it'll be 46 + 7 for the new value which would be 53.

Any ideas on how I can go about achieving this ?

This is what I've tried so far, it replaces the last value of 9 to 6, but it doesn't add 7 to that value it just detects the last value of 9 and replaces to 6, so that's where I'm having trouble.

```using System;

public class Program
{
public static void Main()
{
int count = 15;
int StartValue = 0;
int increment = 7;
for (int i = 0; i <= count; i++)
{
int FindNine = Add % (10);
if(FindNine == 9)
{ FindNine = 6;}
Console.WriteLine(FindNine);

}
}
}```

Any help on this would be appreciated.

• ### Re: Replace the last number/digit of a value

Hi,

Try :

if(FindNine == 9) Add-=3; that is if the last digit is 9 you'll subtract 3.

• ### Re: Replace the last number/digit of a value

Sep 26, 2016 12:26 PM|Cathy Zou|LINK

Hi rankone.
From your description, I suggest you could use the code below:

```int count = 15;
int StartValue = 0;
int increment = 7;
for (int i = 0; i <= count; i++)
{
int FindNine = Add % (10);
if (FindNine == 9)
{
FindNine = 6;
Console.WriteLine(resul + "*");
}
else
{
}

}```

Best regards
Cathy

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• ### Re: Replace the last number/digit of a value

I think this would work fine, however I have another caveat. How would I go about removing the second last digit from a value instead of the last ?

I think I could accomplish this with string.remove then position of the digit. However I want this to be implied on the next value, and not the value containing the 9. If that makes sense.

So basically I think this can be accomplished if I can find the last two digits of a value instead of just the last one.

• ### Re: Replace the last number/digit of a value

Or it is still a computation ie you could subtract Value%10*10 to remove the dizain digit (if I understand what you want to do ie if you have 164 you want to get 104 ?)

• ### Re: Replace the last number/digit of a value

Basically if 9 + 7 = 16, Where the value is 16, I want to drop the 1 to make it 6.

• ### Re: Replace the last number/digit of a value

#### PatriceSc

Or it is still a computation ie you could subtract Value%10*10 to remove the dizain digit (if I understand what you want to do ie if you have 164 you want to get 104 ?)

I think I found the solution, which would mean basically converting it to a string and change the %10 to %100 for the last two digits. Then if it finds 16, to remove the 1 and retain the 6 by String.Remove function.