javalogo

Konversi angka ke huruf terbilang di Java


How to convert number to amount in words using java?

public String angka(int satuan) {

String[] huruf = { “”, “satu”, “dua”, “tiga”, “empat”, “lima”, “enam”, “tujuh”, “delapan”, “sembilan”, “sepuluh”, “sebelas” };

String hasil = ” “;

if (satuan < 12)

hasil = hasil + huruf[satuan];

else if (satuan < 20)

hasil = hasil + angka(satuan – 10) + ” Belas”;

else if (satuan < 100)

hasil = hasil + angka(satuan / 10) + ” Puluh ” + angka(satuan % 10);

else if (satuan < 200)

hasil = hasil + “Seratus ” + angka(satuan – 100);

else if (satuan < 1000)

hasil = hasil + angka(satuan / 100) + ” Ratus ”  + angka(satuan % 100);

else if (satuan < 2000)

hasil = hasil + “Seribu ” + angka(satuan – 1000);

else if (satuan < 1000000)

hasil = hasil + angka(satuan / 1000) + ” Ribu ” + angka(satuan % 1000);

else if (satuan < 1000000000)

hasil = hasil + angka(satuan / 1000000) + ” Juta ” + angka(satuan % 1000000);

else if (satuan >= 1000000000)

hasil = “Angka terlalu besar, harus kurang dari 1 milyar!”;

return hasil;

}

 

public static String[] explode(String str, String delimiter) {

StringTokenizer st = new StringTokenizer(str, delimiter);

String[] result = new String[st.countTokens()];

for (int i = 0; i < result.length; i++)

result[i] = st.nextToken();

return result;

}

 

public static void main(String args[]) {

Test num = new Test();

System.out.println(“Enter your number: “);

Scanner sc = new Scanner(System.in);

int i = sc.nextInt();

String ab = null;

String[] number = num.explode(i + “”, “,”);

for (int l = 0; l < number.length; l++) {

ab = number[l];

}

System.out.println(ab);

int x = Integer.parseInt(ab);

System.out.println(“Hasilnya\n ” + i + ” = ” + num.angka(x) + ” rupiah”);

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s