Skip to content

Instantly share code, notes, and snippets.

@PUSRISTEK
Created January 28, 2017 13:06
Show Gist options
  • Save PUSRISTEK/9dcab0959eb67c6ac8ab901617ff9f8f to your computer and use it in GitHub Desktop.
Save PUSRISTEK/9dcab0959eb67c6ac8ab901617ff9f8f to your computer and use it in GitHub Desktop.
Fungsi Terbilang
#region "Method Terbilang"
//labelBayarAngsuran.Text = Terbilang(decimal.Parse(textjumang.Text));
string[] satuan = new string[10] { "nol", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan" };
string[] belasan = new string[10] { "sepuluh", "sebelas", "dua belas", "tiga belas", "empat belas", "lima belas", "enam belas", "tujuh belas", "delapan belas", "sembilan belas" };
string[] puluhan = new string[10] { "", "", "dua puluh", "tiga puluh", "empat puluh", "lima puluh", "enam puluh", "tujuh puluh", "delapan puluh", "sembilan puluh" };
string[] ribuan = new string[5] { "", "ribu", "juta", "milyar", "triliyun" };
string Terbilang(Decimal d)
{
string strHasil = "";
Decimal frac = d - Decimal.Truncate(d);
if (Decimal.Compare(frac, 0.0m) != 0)
strHasil = Terbilang(Decimal.Round(frac * 100)) + " sen";
else
strHasil = "rupiah";
int xDigit = 0;
int xPosisi = 0;
string strTemp = Decimal.Truncate(d).ToString();
for (int i = strTemp.Length; i > 0; i--)
{
string tmpx = "";
xDigit = Convert.ToInt32(strTemp.Substring(i - 1, 1));
xPosisi = (strTemp.Length - i) + 1;
switch (xPosisi % 3)
{
case 1:
bool allNull = false;
if (i == 1)
tmpx = satuan[xDigit] + " ";
else if (strTemp.Substring(i - 2, 1) == "1")
tmpx = belasan[xDigit] + " ";
else if (xDigit > 0)
tmpx = satuan[xDigit] + " ";
else
{
allNull = true;
if (i > 1)
if (strTemp.Substring(i - 2, 1) != "0")
allNull = false;
if (i > 2)
if (strTemp.Substring(i - 3, 1) != "0")
allNull = false;
tmpx = "";
}
if ((!allNull) && (xPosisi > 1))
if ((strTemp.Length == 4) && (strTemp.Substring(0, 1) == "1"))
tmpx = "se" + ribuan[(int)Decimal.Round(xPosisi / 3m)] + " ";
else
tmpx = tmpx + ribuan[(int)Decimal.Round(xPosisi / 3)] + " ";
strHasil = tmpx + strHasil;
break;
case 2:
if (xDigit > 0)
strHasil = puluhan[xDigit] + " " + strHasil;
break;
case 0:
if (xDigit > 0)
if (xDigit == 1)
strHasil = "seratus " + strHasil;
else
strHasil = satuan[xDigit] + " ratus " + strHasil;
break;
}
}
strHasil = strHasil.Trim().ToLower();
if (strHasil.Length > 0)
{
strHasil = strHasil.Substring(0, 1).ToUpper() +
strHasil.Substring(1, strHasil.Length - 1);
}
return strHasil;
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment