3
I have a cyclic function where a string may have any value in the checker digit, but all future submissions receive a pre-calculated value.
In this role, I receive a string
(which will always be a number) and in this method, I remove the last digit and sum the remainder. In the end, I return this number + 1 and conclude with the sum. Complicated as I explained, no?
Explaining it better would be this: I receive the string 10000 and the return should be 10011. Where 1000 added + 1 and the next last digit is the sum of the rest (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);
}
first: This method needs improvements and/or is "misspelled"?
2nd: Using this method it is possible that the return is 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 in started with 10000, the return will be 10011 then the next time the method is run to string will have the value of 10011, and so on.
These numbers you mentioned are dealt with before. My real fear was that I might have some problem (which I didn’t see). This algorithm was requested by our technicians to meet a customization. I particularly find it a "gambiarra" for the proposed functionality, but that’s beside the point. Thank you very much for the answer.
– Randrade