Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Code Problem: Revision Numbers and Letters

Tags:

c#

I'm trying to write a function that takes a revision number (int) and turns it into a revision name (string). The formula should produce outputs similar to this:

Number  Name
1       A
2       B
3       C
...     ...
25      Y
26      Z
27      AA
28      AB
29      AC
...     ...
51      AY
52      AZ
53      BA
54      BB
55      BC
...     ...

This seems simple, but I think it might involve recursion and I'm terrible at that. Any suggestions?

like image 287
Matthew Jones Avatar asked Dec 06 '22 04:12

Matthew Jones


1 Answers

I think this is the same as working out the Excel column name from a column number:

private string GetExcelColumnName(int columnNumber)
{
    int dividend = columnNumber;
    string columnName = String.Empty;
    int modulo;

    while (dividend > 0)
    {
        modulo = (dividend - 1) % 26;
        columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
        dividend = (int)((dividend - modulo) / 26);
    } 

    return columnName;
}
like image 171
Graham Avatar answered Dec 25 '22 15:12

Graham