Tuesday, December 30, 2008

Terbilang (Java)

Here's Terbilang.java, it's to convert decimal notation into word (Indonesia).

Example:

100 ==> one hundred (seratus)
1020 ==> one thousand and twenty (seribu dua puluh)

So it's very useful for people that want to learning Indonesia ^^.



public class Terbilang {
public static String bilang(long uang) {
String nama[]={"nol","satu","dua","tiga","empat","lima","enam","tujuh","delapan","sembilan"};
String besar[]={"triliun","milyar","juta","ribu",""};
if(uang==0) return nama[0];
long p = 1000000000000l;
String hasil = "";
for(int i=0;i < besar.length;i++,p /= 1000)
{
if(uang < p) continue;
long temp = uang / p;
boolean seribu = p==1000;
if(temp>=100) {
hasil += nama[(int)temp/100] + " ratus ";
temp %= 100;
seribu = false;
}
if(temp>=11 && temp<=19) {
hasil += nama[(int)temp-10] + " belas ";
temp = 0;
seribu = false;
}
if(temp>=10) {
hasil += nama[(int)temp/10] + " puluh ";
temp %= 10;
}
if(temp > 0) {
if(seribu && temp==1)
hasil += "se";
else
hasil += nama[(int)temp] + " ";
}
uang %= p;
hasil += besar[i] + " ";
}
hasil=hasil.replaceAll("satu ratus", "seratus");
hasil=hasil.replaceAll("satu belas", "sebelas");
hasil=hasil.replaceAll("satu puluh", "sepuluh");
return hasil.trim();
}
}

2 comments:

  1. java.lang.NoSuchMethodError: main
    Exception in thread "main"
    Process completed.

    how to fix this problem?

    ReplyDelete
  2. thanks.

    working properly

    ReplyDelete