Convert integers to written numbers
Solution 1:
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);
if (n % 1000 == 0)
{
return friendlyInt;
}
}
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 fix a bug w/ million, billion, etc.)
Solution 2:
I use this handy library called Humanizer.
https://github.com/Humanizr/Humanizer
It supports several cultures and converts not only numbers to words but also date and it's very simple to use.
Here's how I use it:
int someNumber = 543;
var culture = System.Globalization.CultureInfo("en-US");
var result = someNumber.ToWords(culture); // 543 -> five hundred forty-three
And voilá!
Solution 3:
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
Here is the code in c#
public static string AmountInWords(double amount)
{
var n = (int)amount;
if (n == 0)
return "";
else if (n > 0 && n <= 19)
{
var arr = new string[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
return arr[n - 1] + " ";
}
else if (n >= 20 && n <= 99)
{
var arr = new string[] { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
return arr[n / 10 - 2] + " " + AmountInWords(n % 10);
}
else if (n >= 100 && n <= 199)
{
return "One Hundred " + AmountInWords(n % 100);
}
else if (n >= 200 && n <= 999)
{
return AmountInWords(n / 100) + "Hundred " + AmountInWords(n % 100);
}
else if (n >= 1000 && n <= 1999)
{
return "One Thousand " + AmountInWords(n % 1000);
}
else if (n >= 2000 && n <= 999999)
{
return AmountInWords(n / 1000) + "Thousand " + AmountInWords(n % 1000);
}
else if (n >= 1000000 && n <= 1999999)
{
return "One Million " + AmountInWords(n % 1000000);
}
else if (n >= 1000000 && n <= 999999999)
{
return AmountInWords(n / 1000000) + "Million " + AmountInWords(n % 1000000);
}
else if (n >= 1000000000 && n <= 1999999999)
{
return "One Billion " + AmountInWords(n % 1000000000);
}
else
{
return AmountInWords(n / 1000000000) + "Billion " + AmountInWords(n % 1000000000);
}
}