Number Pronouncer
Lagi megang modul finance, ada kebutuhan untuk bikin penterjemah angka menjadi terbilang dalam Bahasa Indonesia. Codenya masih butuh refactoring kalo gw sempat nanti gw beresin. Berikut code lengkapnya:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Class1
{
private string[] _ZeroToEleven = {"Nol", "Satu", "Dua", "Tiga", "Empat"
, "Lima", "Enam", "Tujuh", "Delapan"
, "Sembilan", "Sepuluh", "Sebelas"};
private string[] _GroupSuffix = {"", "ribu", "juta", "milyar", "trilyun", "bilyun", "tetra bilyun"};
public string Pronounce(long Number, bool LowerCase)
{
string numberString = Number.ToString();
string result = string.Empty;
if (Number <= 11)
{
return (LowerCase ? _ZeroToEleven[Number].ToLower() : _ZeroToEleven[Number]);
}
//handle puluhan and belasan but starts from twelve up
if (numberString.Length == 2)
{
int firstDigit = Convert.ToInt32(numberString[0].ToString());
int secondDigit = Convert.ToInt32(numberString[1].ToString());
//belasan (12 - 19)
if (firstDigit == 1)
{
return Pronounce(secondDigit, LowerCase) +" belas";
}
if (secondDigit == 0)
{
return Pronounce(firstDigit, LowerCase) + " puluh";
}
else
{
return Pronounce(firstDigit, LowerCase) + " puluh " + Pronounce(secondDigit, true);
}
}
//handle 100 - 999
if (numberString.Length == 3)
{
long firstDigit = Convert.ToInt32(numberString[0].ToString());
string lastTwoDigits = numberString.Substring(1);
long lastTwoDigitNumber = Convert.ToInt64(lastTwoDigits);
if (firstDigit == 1)
{
if (lastTwoDigitNumber > 0)
{
result = "Seratus " + Pronounce(lastTwoDigitNumber, true);
}
else
{
result = "Seratus";
}
if (LowerCase)
{
result = result[0].ToString().ToLower() + result.Substring(1);
}
return result;
}
else
{
if (lastTwoDigitNumber > 0)
{
return Pronounce(firstDigit, LowerCase) + " ratus " + Pronounce(lastTwoDigitNumber, true);
}
else
{
return Pronounce(firstDigit, LowerCase) + " ratus";
}
}
}
Stack<string> groupStack = GroupNumber(numberString);
if(groupStack.Count > _GroupSuffix.Length)
{
throw new InvalidOperationException("Number is too big. I don't know how to pronounce it in Bahasa.");
}
while (groupStack.Count > 0)
{
string currentGroup = groupStack.Pop();
long currentGroupNumber = Convert.ToInt64(currentGroup);
//Print only 1000 down with 'se' e.g. Seribu
if (currentGroupNumber == 1 && groupStack.Count <= 1)
{
if (result.Length > 0)
{
result += " ";
}
result += "se" + _GroupSuffix[groupStack.Count];
}
else
{
if (currentGroupNumber > 0)
{
if (result.Length > 0)
{
result += " ";
}
result += Pronounce(currentGroupNumber, true) + " " + _GroupSuffix[groupStack.Count];
}
}
}
result = result[0].ToString().ToUpper() + result.Substring(1);
return result;
}
private Stack<string> GroupNumber(string NumberString)
{
Stack<string> groupStack = new Stack<string>();
string currentGroup = string.Empty;
int counter = 0;
for (int i = NumberString.Length - 1; i >= 0; i--)
{
currentGroup = NumberString
.ToString() + currentGroup;
counter++;
if (counter == 3)
{
groupStack.Push(currentGroup);
counter = 0;
currentGroup = string.Empty;
}
}
if (counter > 0)
{
groupStack.Push(currentGroup);
}
return groupStack;
}
}
}
Buat yang butuh silahkan pakai. Kalau ada bug please kasih tahu yaah...!.