Created
April 11, 2016 07:33
-
-
Save adilkurniaramdan/5e3a562eff6aff941bf94dde537f8d57 to your computer and use it in GitHub Desktop.
terbilang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.qinti.eptp.util; | |
/** | |
* Created by adildramdan on 5/6/15. | |
*/ | |
/** | |
* This class will convert numeric values into an english representation | |
* <p> | |
* For units, see : http://www.jimloy.com/math/Milyar.htm | |
* | |
* @author [email protected] | |
*/ | |
public class NumberToWords { | |
public static String terbilang(Long number) { | |
String terbilang = ""; | |
if (number < 0) { | |
terbilang += "Minus "; | |
number = Math.abs(number); | |
} | |
String bilangan = number.toString(); | |
if (bilangan.equals("0")) { | |
terbilang = "Nol"; | |
} else { | |
int jumlahDigit = NumberToWords.berapaDigit(bilangan); | |
switch (jumlahDigit) { | |
case 1: | |
terbilang = NumberToWords.satuDigit(bilangan); | |
break; | |
case 2: | |
terbilang = NumberToWords.duaDigit(bilangan); | |
break; | |
case 3: | |
terbilang = NumberToWords.tigaDigit(bilangan); | |
break; | |
case 4: | |
terbilang = NumberToWords.empatDigit(bilangan); | |
break; | |
case 5: | |
terbilang = NumberToWords.limaDigit(bilangan); | |
break; | |
case 6: | |
terbilang = NumberToWords.enamDigit(bilangan); | |
break; | |
case 7: | |
terbilang = NumberToWords.tujuhDigit(bilangan); | |
break; | |
case 8: | |
terbilang = NumberToWords.delapanDigit(bilangan); | |
break; | |
case 9: | |
terbilang = NumberToWords.sembilanDigit(bilangan); | |
break; | |
case 10: | |
terbilang = NumberToWords.sepuluhDigit(bilangan); | |
break; | |
case 11: | |
terbilang = NumberToWords.sebelasDigit(bilangan); | |
break; | |
case 12: | |
terbilang = NumberToWords.duabelasDigit(bilangan); | |
break; | |
case 13: | |
terbilang = NumberToWords.tigaBelasDigit(bilangan); | |
break; | |
case 14: | |
terbilang = NumberToWords.empatBelasDigit(bilangan); | |
break; | |
case 15: | |
terbilang = NumberToWords.limaBelasDigit(bilangan); | |
break; | |
} | |
} | |
return terbilang +" Rupiah"; | |
} | |
protected static int berapaDigit(String bilangan) { | |
return bilangan.length(); | |
} | |
protected static String satuDigit(String bilangan) { | |
String terbilang = null; | |
switch (bilangan) { | |
case "1": | |
terbilang = "Satu"; | |
break; | |
case "2": | |
terbilang = "Dua"; | |
break; | |
case "3": | |
terbilang = "Tiga"; | |
break; | |
case "4": | |
terbilang = "Empat"; | |
break; | |
case "5": | |
terbilang = "Lima"; | |
break; | |
case "6": | |
terbilang = "Enam"; | |
break; | |
case "7": | |
terbilang = "Tujuh"; | |
break; | |
case "8": | |
terbilang = "Delapan"; | |
break; | |
case "9": | |
terbilang = "Sembilan"; | |
break; | |
} | |
return terbilang; | |
} | |
protected static String duaDigit(String bilangan) { | |
String terbilang; | |
if (bilangan.charAt(0) == '1') { | |
if (bilangan.charAt(1) == '0') { | |
terbilang = "Sepuluh"; | |
} else if (bilangan.charAt(1) == '1') { | |
terbilang = "Sebelas"; | |
} else { | |
terbilang = NumberToWords.satuDigit(String.valueOf(bilangan | |
.charAt(1))) + " Belas"; | |
} | |
} else { | |
terbilang = NumberToWords.satuDigit(bilangan.substring(0, 1)) | |
+ " Puluh"; | |
if (!bilangan.substring(1).equals("0")) { | |
terbilang = terbilang + " " | |
+ NumberToWords.satuDigit(bilangan.substring(1)); | |
} | |
} | |
return terbilang; | |
} | |
protected static String tigaDigit(String bilangan) { | |
String terbilang = null; | |
if (bilangan.charAt(0) == '1') { | |
terbilang = "Seratus"; | |
if (bilangan.charAt(1) == '0') { | |
if (bilangan.charAt(2) == '0') { | |
} else { | |
terbilang = terbilang | |
+ " " | |
+ NumberToWords.satuDigit(String.valueOf(bilangan | |
.charAt(2))); | |
} | |
} else { | |
terbilang = terbilang + " " | |
+ NumberToWords.duaDigit(bilangan.substring(1)); | |
} | |
} else { | |
terbilang = NumberToWords.satuDigit(bilangan.substring(0, 1)) | |
+ " Ratus"; | |
if (bilangan.charAt(1) == '0') { | |
if (bilangan.charAt(2) == '0') { | |
} else { | |
terbilang = terbilang | |
+ " " | |
+ NumberToWords.satuDigit(String.valueOf(bilangan | |
.charAt(2))); | |
} | |
} else { | |
terbilang = terbilang + " " | |
+ NumberToWords.duaDigit(bilangan.substring(1)); | |
} | |
} | |
return terbilang; | |
} | |
protected static String empatDigit(String bilangan) { | |
String terbilang; | |
if (bilangan.charAt(0) == '1') { | |
if (bilangan.charAt(1) == '0') { | |
if (bilangan.charAt(2) == '0') { | |
if (bilangan.charAt(3) == '0') { | |
terbilang = "Seribu"; | |
} else { | |
terbilang = "Seribu " | |
+ NumberToWords.satuDigit(String | |
.valueOf(bilangan.charAt(3))); | |
} | |
} else { | |
terbilang = "Seribu " | |
+ NumberToWords.duaDigit(bilangan.substring(2)); | |
} | |
} else { | |
terbilang = "Seribu " | |
+ NumberToWords.tigaDigit(bilangan.substring(1)); | |
} | |
} else { | |
if (bilangan.charAt(1) == '0') { | |
if (bilangan.charAt(2) == '0') { | |
if (bilangan.charAt(3) == '0') { | |
terbilang = NumberToWords.satuDigit(String | |
.valueOf(bilangan.charAt(0))) + " Ribu"; | |
} else { | |
terbilang = NumberToWords.satuDigit(String | |
.valueOf(bilangan.charAt(0))) | |
+ " Ribu " | |
+ NumberToWords.satuDigit(String | |
.valueOf(bilangan.charAt(3))); | |
} | |
} else { | |
terbilang = NumberToWords.satuDigit(String | |
.valueOf(bilangan.charAt(0))) | |
+ " Ribu " | |
+ NumberToWords.duaDigit(bilangan.substring(2)); | |
} | |
} else { | |
terbilang = NumberToWords.satuDigit(String.valueOf(bilangan | |
.charAt(0))) | |
+ " Ribu " | |
+ NumberToWords.tigaDigit(bilangan.substring(1)); | |
} | |
} | |
return terbilang; | |
} | |
protected static String limaDigit(String bilangan) { | |
String terbilang = NumberToWords.duaDigit(bilangan.substring(0, 2)) | |
+ " Ribu"; | |
if (!bilangan.substring(2, 5).equals("000")) { | |
if (bilangan.charAt(2) == '0') { | |
if (bilangan.charAt(3) == '0') { | |
terbilang = terbilang + " " | |
+ NumberToWords.satuDigit(bilangan.substring(4)); | |
} else { | |
terbilang = terbilang + " " | |
+ NumberToWords.duaDigit(bilangan.substring(3)); | |
} | |
} else { | |
terbilang = terbilang + " " | |
+ NumberToWords.tigaDigit(bilangan.substring(2)); | |
} | |
} | |
return terbilang; | |
} | |
protected static String enamDigit(String bilangan) { | |
String terbilang = NumberToWords.tigaDigit(bilangan.substring(0, 3)) | |
+ " Ribu"; | |
if (!bilangan.substring(2, 6).equals("0000")) terbilang = terbilang + " " | |
+ NumberToWords.tigaDigit(bilangan.substring(3, 6)); | |
return terbilang; | |
} | |
protected static String tujuhDigit(String bilangan) { | |
String terbilang = NumberToWords.satuDigit(bilangan.substring(0, 1)) | |
+ " Juta"; | |
if (!bilangan.substring(1).equals("000000")) { | |
terbilang = terbilang + " " | |
+ NumberToWords.enamDigit(bilangan.substring(1)); | |
} | |
return terbilang; | |
} | |
protected static String delapanDigit(String bilangan) { | |
String terbilang = NumberToWords.duaDigit(bilangan.substring(0, 2)) | |
+ " Juta"; | |
if (bilangan.substring(2).equals("000000")) { | |
} else { | |
terbilang = terbilang + " " | |
+ NumberToWords.enamDigit(bilangan.substring(2)); | |
} | |
return terbilang; | |
} | |
protected static String sembilanDigit(String bilangan) { | |
String terbilang = NumberToWords.tigaDigit(bilangan.substring(0, 3)) | |
+ " Juta"; | |
if (bilangan.substring(3).equals("000000")) { | |
} else { | |
terbilang = terbilang + " " | |
+ NumberToWords.enamDigit(bilangan.substring(3)); | |
} | |
return terbilang; | |
} | |
protected static String sepuluhDigit(String bilangan) { | |
String terbilang = NumberToWords.satuDigit(bilangan.substring(0, 1)) | |
+ " Miliar"; | |
if (bilangan.substring(1).equals("000000000")) { | |
} else { | |
terbilang = terbilang + " " | |
+ NumberToWords.sembilanDigit(bilangan.substring(1)); | |
} | |
return terbilang; | |
} | |
protected static String sebelasDigit(String bilangan) { | |
String terbilang = NumberToWords.duaDigit(bilangan.substring(0, 2)) | |
+ " Miliar"; | |
if (bilangan.substring(1).equals("000000000")) { | |
} else { | |
terbilang = terbilang + " " | |
+ NumberToWords.sembilanDigit(bilangan.substring(1)); | |
} | |
return terbilang; | |
} | |
protected static String duabelasDigit(String bilangan) { | |
String terbilang = NumberToWords.tigaDigit(bilangan.substring(0, 3)) | |
+ " Miliar"; | |
if (bilangan.substring(1).equals("000000000")) { | |
} else { | |
terbilang = terbilang + " " | |
+ NumberToWords.sembilanDigit(bilangan.substring(1)); | |
} | |
return terbilang; | |
} | |
protected static String tigaBelasDigit(String bilangan) { | |
String terbilang = NumberToWords.satuDigit(bilangan.substring(0, 1)) | |
+ " Triliun"; | |
if (bilangan.substring(1).equals("000000000000")) { | |
} else { | |
terbilang = terbilang + " " | |
+ NumberToWords.duabelasDigit(bilangan.substring(1)); | |
} | |
return terbilang; | |
} | |
protected static String empatBelasDigit(String bilangan) { | |
String terbilang = NumberToWords.duaDigit(bilangan.substring(0, 2)) | |
+ " Triliun"; | |
if (bilangan.substring(1).equals("000000000000")) { | |
} else { | |
terbilang = terbilang + " " | |
+ NumberToWords.duabelasDigit(bilangan.substring(1)); | |
} | |
return terbilang; | |
} | |
protected static String limaBelasDigit(String bilangan) { | |
String terbilang = NumberToWords.tigaDigit(bilangan.substring(0, 3)) | |
+ " Triliun"; | |
if (!bilangan.substring(1).equals("000000000000")) { | |
terbilang = terbilang + " " | |
+ NumberToWords.duabelasDigit(bilangan.substring(1)); | |
} | |
return terbilang; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment