I have a cyclic function where an initial string can have any value in the check digit, but all future submissions receive a previously calculated value.
In this function, I get a string
(which will always be a number) and in this method, I take the last digit and add up the rest. At the end, return that number + 1 and concatenate with the sum. Complicated in this way I explained, no?
Explaining better, this would be it: I get the string 10000 and the return should be 10011. Where 1000 is + 1 and the next last digit is the sum of the remainder (1 + 0 + 0 + 0 = 1).
My method is as follows:
public static int GenerateKey(string s)
{
s = s.Remove(s.Length - 1);
var i = Convert.ToInt32(s);
var sum = 0;
while (i != 0)
{
sum += i % 10;
i /= 10;
}
var somatorio = Convert.ToInt32(s) + 1;
var id = Convert.ToString(somatorio);
if (sum.ToString().Length > 1)
{
sum = sum % 10;
}
id = id += sum;
return Convert.ToInt32(id);
}
1st: This method needs improvement and / or is "poorly written"?
2nd: Using this method is it possible for the return to be repeated with one already used before? Remembering that the next time you execute the method the number will be the return of the previously executed method.
Ex: If I started with 10000, the return will be 10011, so the next time the method is run the string will have the value of 10011, and so on.