Created
November 4, 2013 23:11
-
-
Save omerfarukz/7310926 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
namespace NumberToAlpha | |
{ | |
public static class NumericExtensions | |
{ | |
public static string ToAlphabetic(this int number) | |
{ | |
if (number < 1) | |
throw new ArgumentOutOfRangeException("number"); | |
var minCharCode = Convert.ToChar("A"); | |
var lengthOfAlphabetics = 26; | |
var capacity = (int)Math.Floor((double)number / lengthOfAlphabetics) + 1; | |
var sb = new StringBuilder(capacity); | |
var currentCharCode = number; | |
var prevCharCode = 1; | |
var charCode = -1; | |
while (currentCharCode > lengthOfAlphabetics) | |
{ | |
if (prevCharCode > lengthOfAlphabetics) | |
prevCharCode = 1; | |
charCode = minCharCode + prevCharCode - 1; | |
sb.Append((char)charCode); | |
currentCharCode -= lengthOfAlphabetics; | |
++prevCharCode; | |
} | |
charCode = minCharCode + currentCharCode - 1; | |
sb.Append((char)charCode); | |
return sb.ToString(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace NumberToAlpha | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Calculating..."); | |
var alphabetics = 4000.ToAlphabetic(); // int.MaxValue.ToAlphabetic(); | |
Console.WriteLine(alphabetics); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment