Here is an extension method which formats integer values to your format (with leading letters):
public static string ToZormat(this int value, int length = 5)
{
string map = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] result = new char[length];
var x = value;
for(int i = 0; i < length; i++)
{
int threshold = (int)Math.Pow(10, length - i - 1);
var index = Math.Min(map.Length - 1, x / threshold);
result[i] = map[index];
x -= threshold * index;
}
return new String(result);
}
When you have number formatted to a string of length 5, leading letters appear after value 99,999, next go A0,000, ... A9,999, B0,000 etc. As you can see the first character changes every 10,000 numbers. The second character changes every 1,000 numbers. And last, the fifth character will change for every number. We just need to implement that algorithm.
Basic steps:
- Define map of characters which are used in your format
- Calculate character change threshold for the current position (i) - it will be power of 10:
10,000, 1,000, 100, 10, 1.
- Get the index of character from map. Its simply number of thresholds which fits the value, but not more than the number of characters in your map.
- Calculate what's left from input value and go to next postion
You should add validation for the max value which fits the given length of the formatted string.
Sample for length 3:
Enumerable.Range(0, 3886).Select(x => x.ToZormat(3))
Output:
000
001
002
...
999
A00
A01
...
A99
B00
B01
...
Z99
ZA0
ZA1
...
ZZ9
ZZA
...
ZZZ
Z9999 -> AA001?AZ999 -> BA001?