Convert Number to Words

1. แปลงตัวเลขเป็นคำในภาษาอังกฤษ
2. แปลงตัวเลขเป็นคำในภาษาไทย

1.แปลงตัวเลขเป็นคำในภาษาอังกฤษ

```using System;

namespace ConsoleApplication1
{
class Program
{
// PROGRAM HANDLES NEGATIVE AND POSITIVE DOUBLES

static void Main(string[] args)
{
//Console.Write("Enter a number to convert to words: ");
Double num = 101;

Console.WriteLine("{0}", NumWordsWrapper(num));
}

static String NumWordsWrapper(double num)
{
string words = "";
double intPart;
double decPart = 0;
if (num == 0)
return "zero";
try
{
string[] splitter = num.ToString().Split('.');
intPart = double.Parse(splitter[0]);
decPart = double.Parse(splitter[1]);
}
catch
{
intPart = num;
}

words = NumWords(intPart);

if (decPart > 0)
{
if (words != "")
words += " and ";
int counter = decPart.ToString().Length;
switch (counter)
{
case 1: words += NumWords(decPart) + " tenths"; break;
case 2: words += NumWords(decPart) + " hundredths"; break;
case 3: words += NumWords(decPart) + " thousandths"; break;
case 4: words += NumWords(decPart) + " ten-thousandths"; break;
case 5: words += NumWords(decPart) + " hundred-thousandths"; break;
case 6: words += NumWords(decPart) + " millionths"; break;
case 7: words += NumWords(decPart) + " ten-millionths"; break;
}
}
return words;
}

static String NumWords(double num) //converts double to words
{
string[] numbersArr = new string[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
string[] tensArr = new string[] { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninty" };
string[] suffixesArr = new string[] { "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "Quattuordecillion", "Quindecillion", "Sexdecillion", "Septdecillion", "Octodecillion", "Novemdecillion", "Vigintillion" };
string words = "";

bool tens = false;

if (num < 0)
{
words += "negative ";
num *= -1;
}

int power = (suffixesArr.Length + 1) * 3;

while (power > 3)
{
double pow = Math.Pow(10, power);
if (num >= pow)
{
if (num % pow > 0)
{
words += NumWords(Math.Floor(num / pow)) + " " + suffixesArr[(power / 3) - 1] + ", ";
}
else if (num % pow == 0)
{
words += NumWords(Math.Floor(num / pow)) + " " + suffixesArr[(power / 3) - 1];
}
num %= pow;
}
power -= 3;
}
if (num >= 1000)
{
if (num % 1000 > 0) words += NumWords(Math.Floor(num / 1000)) + " thousand, ";
else words += NumWords(Math.Floor(num / 1000)) + " thousand";
num %= 1000;
}
if (0 <= num && num <= 999)
{
if ((int)num / 100 > 0)
{
words += NumWords(Math.Floor(num / 100)) + " hundred";
num %= 100;
}
if ((int)num / 10 > 1)
{
if (words != "")
words += " ";
words += tensArr[(int)num / 10 - 2];
tens = true;
num %= 10;
}

if (num < 20 && num > 0)
{
if (words != "" && tens == false)
words += " ";
words += (tens ? "-" + numbersArr[(int)num - 1] : numbersArr[(int)num - 1]);
num -= Math.Floor(num);
}
}

return words;

}

}
}```

2.แปลงตัวเลขเป็นคำในภาษาไทย

```using System;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//Console.Write("Enter a number to convert to words: ");
//Double n = Double.Parse(args[0]);
//Double num = 12345.6;
//Console.WriteLine("{0}", NumWordsWrapper(num));

Console.WriteLine("{0}", NumWordsWrapper(0));
Console.WriteLine("{0}", NumWordsWrapper(0.5));
Console.WriteLine("{0}", NumWordsWrapper(1));
Console.WriteLine("{0}", NumWordsWrapper(1.0));
Console.WriteLine("{0}", NumWordsWrapper(10));
Console.WriteLine("{0}", NumWordsWrapper(11.25));
Console.WriteLine("{0}", NumWordsWrapper(123.456));
Console.WriteLine("{0}", NumWordsWrapper(12345.6));
}

public static string NumWordsWrapper(double num)
{
string words = "";
int intPart;
int decPart = 0;

if (num == 0)
return "ศูนย์บาท";

intPart = (int)Math.Floor(num);
decPart = (int)((num - intPart) * 100);

words = NumWords(intPart);
if (words != "") {
words += "บาท";
}

if (decPart > 0)
{
words += NumWords(decPart) + "สตางค์";
}

return words;
}

private static string NumWords(double num) //converts double to words
{
string[] numbersArr = new string[] { "เอ็ด", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า", "สิบ" };
string[] tensArr = new string[] { "สิบ", "ยี่สิบ", "สามสิบ", "สี่สิบ", "ห้าสิบ", "หกสิบ", "เจ็ดสิบ", "แปดสิบ", "เก้าสิบ" };
string words = "";
if (num == 1)
return "หนึ่ง";
if (num < 0)
{
words += "ติดลบ";
num *= -1;
}
if (num >= 1000000)
{
if (num % 1000000 > 0) words += NumWords(Math.Floor(num / 1000000)) + "ล้าน";
else words += NumWords(Math.Floor(num / 1000000)) + "ล้าน";
num %= 1000000;
}
if (num >= 100000)
{
words += NumWords(Math.Floor(num / 100000)) + "แสน";
num %= 100000;
}
if (num >= 10000)
{
words += NumWords(Math.Floor(num / 10000)) + "หมื่น";
num %= 10000;
}
if (num >= 1000)
{
words += NumWords(Math.Floor(num / 1000)) + "พัน";
num %= 1000;
}
if (num >= 100)
{
words += NumWords(Math.Floor(num / 100)) + "ร้อย";
num %= 100;
}

if (num >= 10)
{
words += tensArr[Convert.ToInt32(Math.Floor(num / 10)) - 1];
num %= 10;
}
if (num >= 1)
{
words += numbersArr[Convert.ToInt32(Math.Floor(num)) - 1];
num -= Math.Floor(num);
}

return words;
}
}
}```

จะได้

```ศูนย์บาท
ห้าสิบสตางค์
หนึ่งบาท
หนึ่งบาท
สิบบาท
สิบเอ็ดบาทยี่สิบห้าสตางค์
หนึ่งร้อยยี่สิบสามบาทสี่สิบห้าสตางค์
หนึ่งหมื่นสองพันสามร้อยสี่สิบห้าบาทหกสิบสตางค์```