ກ່ອນອື່ນຕ້ອງຕັດໂຕເລກອອກມາເປັນຫົວໜ່ວຍ(1-9),ສິບ,ຮ້ອຍ,ພັນ,ໝື່ນ,ແສນ,ລ້ານ... ແລ້ວ ຄ່ອຍມາຕໍ່ກັນ
ຕົວຢ່າງ (C#) :
ກ່ອນອື່ນສ້າງຟັງຊັນສຳຫຼັບຫົວໜ່ວຍກ່ອນ
private static String ones(String Number)
{
int _Number = Convert.ToInt32(Number);
String name = "";
switch (_Number)
{
case 1:
name = "ໜຶ່ງ";
break;
case 2:
name = "ສອງ";
break;
case 3:
name = "ສາມ";
break;
case 4:
name = "ສີ່";
break;
case 5:
name = "ຫ້າ";
break;
case 6:
name = "ຫົກ";
break;
case 7:
name = "ເຈັດ";
break;
case 8:
name = "ແປດ";
break;
case 9:
name = "ເກົ້າ";
break;
}
return name;
}
ຈາກນັ້ນສ້າງຟັງຊັນຫົວສິບຕັ້ງແຕ່ 10,11,12-20 ແລ້ວຕໍ່່ດ້ວຍ 30,40,50 - 100 ດັ່ງນີ້
private static String tens(String Number)
{
int _Number = Convert.ToInt32(Number);
String name = null;
switch (_Number)
{
case 10:
name = "ສິບ";
break;
case 11:
name = "ສິບເອັດ";
break;
case 12:
name = "ສິບສອງ";
break;
case 13:
name = "ສິບສາມ";
break;
case 14:
name = "ສິບສີ່";
break;
case 15:
name = "ສິບຫ້າ";
break;
case 16:
name = "ສິບຫົກ";
break;
case 17:
name = "ສິບເຈັດ";
break;
case 18:
name = "ສິບແປດ";
break;
case 19:
name = "ສິບເກົ້າ";
break;
case 20:
name = "ຊາວ";
break;
case 21:
name = 'ຊາວເອັດ';
break;
case 30:
name = "ສາມສິບ";
break;
case 31:
name = "ສາມສິບເອັດ";
break;
case 40:
name = "ສີ່ສິບ";
break;
case 41:
name = "ສີ່ສິບເອັດ";
break;
case 50:
name = "ຫ້າສິບ";
break;
case 51:
name = "ຫ້າສິບເອັດ";
break;
case 60:
name = "ຫົກສິບ";
break;
case 61:
name = "ຫົກສິບເອັດ";
break;
case 70:
name = "ເຈັດສິບ";
break;
case 71:
name = "ເຈັດສິບເອັດ";
break;
case 80:
name = "ແປດສິບ";
break;
case 81:
name = "ແປດສິບເອັດ";
break;
case 90:
name = "ເກົ້າສິບ";
break;
case 91:
name = "ເກົ້າສິບເອັດ";
break;
default:
if (_Number > 0)
{
name = tens(Number.Substring(0, 1) + "0") + " " + ones(Number.Substring(1));
}
break;
}
return name;
}
ຫຼັງຈາກນັ້ນໃຫ້ສ້າງອີກຟັງຊັ່ນເພື່ອນັບຫົວຮ້ອຍ,ພັນລ້ານ... ແລ້ວລວມອອກມາເປັນພາສາເວົ້າດັ່ງນີ້:
private static String ConvertWholeNumber(String Number)
{
string word = "";
try
{
bool beginsZero = false;//tests for 0XX
bool isDone = false;//test if already translated
double dblAmt = (Convert.ToDouble(Number));
//if ((dblAmt > 0) && number.StartsWith("0"))
if (dblAmt > 0)
{//test for zero or digit zero in a nuemric
beginsZero = Number.StartsWith("0");
int numDigits = Number.Length;
int pos = 0;//store digit grouping
String place = "";//digit grouping name:hundres,thousand,etc...
switch (numDigits)
{
case 1://ones' range
word = ones(Number);
isDone = true;
break;
case 2://tens' range
word = tens(Number);
isDone = true;
break;
case 3://hundreds' range
pos = (numDigits % 3) + 1;
place = " ຮ້ອຍ ";
break;
case 4://thousands' range
case 5:
case 6:
pos = (numDigits % 4) + 1;
place = " ພັນ ";
break;
case 7:
pos = (numDigits % 5) + 1;
place = " ໝືນ ";
break;
case 8:
pos = (numDigits % 6) + 1;
place = " ແສນ ";
break;
case 9:
pos = (numDigits % 7) + 1;
place = " ລ້ານ ";
break;
case 10://Billions's range
case 11:
case 12:
pos = (numDigits % 10) + 1;
place = " ສິບລ້ານ ";
break;
//add extra case options for anything above Billion...
default:
isDone = true;
break;
}
if (!isDone)
{//if transalation is not done, continue...(Recursion comes in now!!)
if (Number.Substring(0, pos) != "0" && Number.Substring(pos) != "0")
{
try
{
word = ConvertWholeNumber(Number.Substring(0, pos)) + place + ConvertWholeNumber(Number.Substring(pos));
}
catch { }
}
else
{
word = ConvertWholeNumber(Number.Substring(0, pos)) + ConvertWholeNumber(Number.Substring(pos));
}
//check for trailing zeros
//if (beginsZero) word = " and " + word.Trim();
}
//ignore digit grouping names
if (word.Trim().Equals(place.Trim())) word = "";
}
}
catch { }
return word.Trim(); }
ເວລານຳໃຊ້ແມ່ນ:
string strNumtoRead = '212';
string result = ConvertWholeNumber(strNumtoRead ).Trim();
-- result = ສອງຮ້ອຍສິບສອງ