SHARE
TWEET

angka to kata

a guest Aug 23rd, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2. import java.util.Scanner;
  3.  
  4. public class NumberToWordConverter {
  5.  
  6.    public static void main(String[] args) {
  7.     int number = 0;
  8.     Scanner scanner = new Scanner(System.in);
  9.     System.out.println("Ketik angkanya(max  9 digits doang)");
  10.     try {
  11.            // baca nomornya
  12.        number = scanner.nextInt();
  13.        if (number == 0) {
  14.         System.out.print("Angka ke Kata: Nol");
  15.        } else {
  16.         System.out.print("Angka ke Kata: " + numberToWord(number));
  17.        }
  18.      } catch (Exception e) {
  19.         System.out.println("Please enter a valid number");
  20.      }
  21.     // tutup membaca nomor
  22.     scanner.close();
  23.    }
  24.  
  25.    private static String numberToWord(int number) {
  26.         // variabel untuk menahan representasi string angka
  27.         String words = "";
  28.         String unitsArray[] = { "zero", "satu", "dua", "tiga", "empat", "lima", "enam",
  29.                       "tujuh", "delapan", "sembilan", "sepuluh", "sebelas", "duabelas",
  30.                       "tigabelas", "empatbelas", "limabelas", "enambelas", "tujuhbelas",
  31.                       "delapanbelas", "sembilanbelas" };
  32.     String tensArray[] = { "zero", "sepuluh", "duapuluh", "tigapuluh", "empatpuluh", "limapuluh",
  33.                       "enampuluh", "tujuhpuluh", "delapanpuluh", "sembilanpuluh" };
  34.  
  35.     if (number == 0) {
  36.         return "zero";
  37.     }
  38.     // tambahkan minus sebelum konversi jika jumlahnya kurang dari 0
  39.     if (number < 0) { // convert the number to a string
  40.            String numberStr = "" + number;
  41.            // hapus minus sebelum nomor tersebut
  42.            numberStr = numberStr.substring(1);
  43.            // tambahkan minus sebelum nomor dan konversi sisa nomor
  44.            return "minus " + numberToWord(Integer.parseInt(numberStr));
  45.         }
  46.         // periksa apakah jumlahnya dapat dibagi 1 juta
  47.         if ((number / 1000000) > 0) {
  48.        words += numberToWord(number / 1000000) + " juta ";
  49.        number %= 1000000;
  50.     }
  51.     // periksa apakah jumlahnya habis dibagi 1 ribu
  52.     if ((number / 1000) > 0) {
  53.         words += numberToWord(number / 1000) + " ribu ";
  54.         number %= 1000;
  55.     }
  56.     // periksa apakah jumlahnya habis dibagi seratus
  57.     if ((number / 100) > 0) {
  58.          words += numberToWord(number / 100) + " ratus ";
  59.          number %= 100;
  60.     }
  61.  
  62.     if (number > 0) {
  63.          // periksa apakah jumlahnya habis dibagi belasan
  64.          if (number < 20) {
  65.                 // ambil nilai yang sesuai dari unit array
  66.                 words += unitsArray[number];
  67.              } else {
  68.                 // ambil nilai yang sesuai dari puluhan array
  69.                 words += tensArray[number / 10];
  70.                 if ((number % 10) > 0) {
  71.             words += "-" + unitsArray[number % 10];
  72.                 }  
  73.          }
  74.           }
  75.  
  76.       return words;
  77.    }
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top