Thursday, February 3, 2011

C# Convert Integers into Written Numbers

Is there an efficient method of converting an integer into the written numbers for example:

String Written = IntegerToWritten(21);

would return "Twenty One"

Is there any way of doing this that doesn't involve a massive lookup table?

  • why massive lookup table?

    string GetWrittenInteger(int n)
    {
      string[] a = new string[] {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" }
      string[] b = new string[] { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }
      string[] c = new string[] {"Twenty", "Thirty", "Forty", "Sixty", "Seventy", "Eighty", "Ninety"};
      string[] d = new string[] {"Hundred", "Thousand", "Million"}
    
      string s = n.ToString();
      for (int i = 0; i < s.Length; i++)
      {
        // logic (too lazy but you get the idea)
      }
    }
    
  • Justin Rogers has a "NumbersToEnglish" class which should do the job for you nicely!

    Initial posting.
    http://weblogs.asp.net/justin_rogers/archive/2004/06/09/151675.aspx

    Finalized Source Code
    http://weblogs.asp.net/justin_rogers/articles/151757.aspx

    It does have a bit of an internal lookup table but I don't really know how you are going to be able to get away from that.

    From Calanus
  • Here are the correct links to the posts by Justin Rogers: initial post, code only.

    There is also an article on CodeProject that addresses this issue.

  • I use this code.It is VB code but you can easily translate it to C#. It works

    Function NumberToText(ByVal n As Integer) As String

    Select Case n
    Case 0
    Return ""

    Case 1 To 19
    Dim arr() As String = {"One","Two","Three","Four","Five","Six","Seven", _
    "Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen", _
    "Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}
    Return arr(n-1) & " "

    Case 20 to 99
    Dim arr() as String = {"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}
    Return arr(n\10 -2) & " " & NumberToText(n Mod 10)

    Case 100 to 199
    Return "One Hundred " & NumberToText(n Mod 100)

    Case 200 to 999
    Return NumberToText(n\100) & "Hundreds " & NumberToText(n mod 100)

    Case 1000 to 1999
    Return "One Thousand " & NumberToText(n Mod 1000)

    Case 2000 to 999999
    Return NumberToText(n\1000) & "Thousands " & NumberToText(n Mod 1000)

    Case 1000000 to 1999999
    Return "One Million " & NumberToText(n Mod 1000000)

    Case 1000000 to 999999999
    Return NumberToText(n\1000000) & "Millions " & NumberToText(n Mod 1000000)

    Case 1000000000 to 1999999999
    Return "One Billion " & NumberTotext(n Mod 1000000000)

    Case Else
    Return NumberToText(n\1000000000) & "Billion " _
    & NumberToText(n mod 1000000000)
    End Select
    End Function
    From Nick Masao
  • This should work reasonably well:

    public static class HumanFriendlyInteger
    {
    static string[] ones = new string[] { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
    static string[] teens = new string[] { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
    static string[] tens = new string[] { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
    static string[] thousandsGroups = { "", " Thousand", " Million", " Billion" };

    private static string FriendlyInteger(int n, string leftDigits, int thousands)
    {
    if (n == 0)
    {
    return leftDigits;
    }
    string friendlyInt = leftDigits;
    if (friendlyInt.Length > 0)
    {
    friendlyInt += " ";
    }

    if (n < 10)
    {
    friendlyInt += ones[n];
    }
    else if (n < 20)
    {
    friendlyInt += teens[n - 10];
    }
    else if (n < 100)
    {
    friendlyInt += FriendlyInteger(n % 10, tens[n / 10 - 2], 0);
    }
    else if (n < 1000)
    {
    friendlyInt += FriendlyInteger(n % 100, (ones[n / 100] + " Hundred"), 0);
    }
    else
    {
    friendlyInt += FriendlyInteger(n % 1000, FriendlyInteger(n / 1000, "", thousands+1), 0);
    }

    return friendlyInt + thousandsGroups[thousands];
    }

    public static string IntegerToWritten(int n)
    {
    if (n == 0)
    {
    return "Zero";
    }
    else if (n < 0)
    {
    return "Negative " + IntegerToWritten(-n);
    }

    return FriendlyInteger(n, "", 0);
    }

    }

    (Edited to make it considerably more concise.)

    John Nolan : I want to see the internationalised version.
    Christian Hayter : It's interesting to note how many tiny differences there are between the above (US English) and a UK English equivalent, let alone other languages... :-)
    From Wedge

0 comments:

Post a Comment