Skip to content

Instantly share code, notes, and snippets.

@imam-san
Last active November 16, 2016 03:15
Show Gist options
  • Save imam-san/7b9a17b4a87c73f4abe963ed0c47862f to your computer and use it in GitHub Desktop.
Save imam-san/7b9a17b4a87c73f4abe963ed0c47862f to your computer and use it in GitHub Desktop.
try amount string to Text in indonesian
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package amounttotext;
/**
*
* @author san
*/
public class AmtDefine {
public static String SEPULUH="sepuluh";
public static String SEBELAS ="sebelas";
public static String DUABELAS ="duabelas";
public static String TIGABELAS ="tigabelas";
public static String EMPATBELAS ="empatbelas";
public static String LIMABELAS ="limabelas";
public static String ENAMBELAS ="enambelas";
public static String TUJUHBELAS ="tujuhbelas";
public static String DELAPANBELAS ="delapanbelas";
public static String SEMBILANBELAS ="sembilanbelas";
public static String SE___="se";
public static String SERATUS="seratus";
public static String SERIBU="seribu";
public static String SATU="satu";
public static String DUA="dua";
public static String TIGA="tiga";
public static String EMPAT="empat";
public static String LIMA="lima";
public static String ENAM="enam";
public static String TUJUH="tujuh";
public static String DELAPAN="delapan";
public static String SEMBILAN="sembilan";
public static String NOL="nol";
public static String JUTA="juta";
public static String PULUH="puluh";
public static String RATUS="ratus";
public static String RIBU="ribu";
public static String BELAS="belas";
public static String RUPIAH="rupiah";
}
import static com.id.amttotext.AmtDefine.*;
/**
*
* @author san
*/
public class AmttoText {
private String input;
private int len;
private String output;
private int selectDot;
private boolean anomalisebelas =false;
private boolean anomalisebelasratus =false;
private boolean anomalisebelasjuta =false;
private boolean ratusanribu=false;
private boolean puluhanribu=false;
private boolean belasanjuta=false;
private boolean puluhanjuta=false;
private boolean anomalisebelasribu= false;
// maximum 100.000.000
public AmttoText(String input)
{
this.input=input;
}
public static boolean isNumeric(String str)
{
double maximumAmt=100000000;
try
{
double d = Double.parseDouble(str);
if (d>maximumAmt)
return false;
}
catch(NumberFormatException nfe)
{
return false;
}
return true;
}
private String processDetailBelasan(String origin)
{
StringBuilder sb= new StringBuilder();
if(origin.equals("10"))
sb.append(SEPULUH);
if(origin.equals("11"))
sb.append(SEBELAS);
if(origin.equals("12"))
sb.append(DUABELAS);
if(origin.equals("13"))
sb.append(TIGABELAS);
if(origin.equals("14"))
sb.append(EMPATBELAS);
if(origin.equals("15"))
sb.append(LIMABELAS);
if(origin.equals("16"))
sb.append(ENAMBELAS);
if(origin.equals("17"))
sb.append(TUJUHBELAS);
if(origin.equals("18"))
sb.append(DELAPANBELAS);
if(origin.equals("19"))
sb.append(SEMBILANBELAS);
return sb.toString();
}
private String processDetail8(char charAt, int lenremain, String remainStr)
{
// System.out.println("Detail 8 ="+ lenremain + "=" + remainStr + " char "+charAt );
String localstr="";
switch (charAt)
{
case '0': //localstr= NOL;
break;
case '1':
//anomalisebelas=true;
anomalisebelasjuta=true;
String checkStr=remainStr.substring(0, 1);
localstr= processDetailBelasan(charAt+checkStr);
//localstr= SATU;
localstr=localstr +" "+ JUTA;
break;
case '2':localstr= DUA+" " +PULUH;break;
case '3':localstr= TIGA+" " +PULUH;break;
case '4':localstr= EMPAT+" " +PULUH;break;
case '5':localstr= LIMA+" " +PULUH;break;
case '6':localstr= ENAM+" " +PULUH;break;
case '7':localstr= TUJUH+" " +PULUH;break;
case '8':localstr= DELAPAN+" " +PULUH;break;
case '9':localstr= SEMBILAN+" " +PULUH;break;
}
belasanjuta=true;
return localstr;
}
private String processDetail7(char charAt, int lenremain, String remainStr)
{
// System.out.println("Detail 7 ="+ lenremain + "=" + remainStr + " char "+charAt );
//1098puluhanjuta=true;
String localstr="";
if (anomalisebelasjuta)
return localstr;
switch (charAt)
{
case '0': //localstr= NOL;
break;
case '1':
//anomalisebelas=true;
// anomalisebelasjuta=true;
// String checkStr=remainStr.substring(0, 1);
// localstr= processDetailBelasan(charAt+checkStr);
// //localstr= SATU;
localstr=SATU +" "+ JUTA;
break;
case '2':localstr= DUA+" " +JUTA;break;
case '3':localstr= TIGA+" " +JUTA;break;
case '4':localstr= EMPAT+" " +JUTA;break;
case '5':localstr= LIMA+" " +JUTA;break;
case '6':localstr= ENAM+" " +JUTA;break;
case '7':localstr= TUJUH+" " +JUTA;break;
case '8':localstr= DELAPAN+" " +JUTA;break;
case '9':localstr= SEMBILAN+" " +JUTA;break;
}
return localstr;
}
private String processDetail6(char charAt, int lenremain, String remainStr)
{
// System.out.println("Detail 6 ="+ lenremain + "=" + remainStr + " char "+charAt );
String localstr="";
//if (anomalisebelasjuta)
// return localstr;
switch (charAt)
{
case '0': //localstr= NOL;
break;
case '1':
anomalisebelasratus=true;
// String checkStr=remainStr.substring(0, 1);
// localstr= processDetailBelasan(charAt+checkStr);
//localstr= SATU;
localstr=SE___ +""+ RATUS+" ";
// localstr= SATU+" " +JUTA;
//localstr= SATU;
break;
case '2':localstr= DUA+" " +RATUS+" ";break;
case '3':localstr= TIGA+" " +RATUS+" ";break;
case '4':localstr= EMPAT+" " +RATUS+" ";;break;
case '5':localstr= LIMA+" " +RATUS+" ";break;
case '6':localstr= ENAM+" " +RATUS+" ";break;
case '7':localstr= TUJUH+" " +RATUS+" ";break;
case '8':localstr= DELAPAN+" " +RATUS+" ";break;
case '9':localstr= SEMBILAN+" " +RATUS+" ";break;
}
ratusanribu=true;
return localstr;
}
private String processDetail5(char charAt, int lenremain, String remainStr)
{
// System.out.println("Detail 5 ="+ lenremain + "=" + remainStr + " char "+charAt );
String localstr="";
switch (charAt)
{
case '0': //localstr= NOL;
anomalisebelasribu=true;
break;
case '1':
//localstr= SE___+"" +PULUH+" ";
//localstr= SATU;
anomalisebelasribu=true;
String checkStr=remainStr.substring(0, 1);
localstr= processDetailBelasan(charAt+checkStr);
//localstr= SATU;
localstr=localstr +" ";
//puluhanribu=false;
break;
case '2':localstr= DUA+" " +PULUH+" ";break;
case '3':localstr= TIGA+" " +PULUH+" ";break;
case '4':localstr= EMPAT+" " +PULUH+" ";break;
case '5':localstr= LIMA+" " +PULUH+" ";break;
case '6':localstr= ENAM+" " +PULUH+" ";break;
case '7':localstr= TUJUH+" " +PULUH+" ";break;
case '8':localstr= DELAPAN+" " +PULUH+" ";break;
case '9':localstr= SEMBILAN+" " +PULUH+" ";break;
}
puluhanribu=true;
return localstr;
}
private String processDetail4(char charAt, int lenremain, String remainStr)
{
// System.out.println("Detail 4 ="+ lenremain + "=" + remainStr + " char "+charAt );
String localstr="";
switch (charAt)
{
case '0': //localstr= NOL;
break;
case '1':
if (input.length()==4)
localstr= SE___+"" +RIBU;
else
localstr= SATU+ RIBU;
//localstr= SATU;
break;
case '2':localstr= DUA+" " +RIBU;break;
case '3':localstr= TIGA+" " +RIBU;break;
case '4':localstr= EMPAT+" " +RIBU;break;
case '5':localstr= LIMA+" " +RIBU;break;
case '6':localstr= ENAM+" " +RIBU;break;
case '7':localstr= TUJUH+" " +RIBU;break;
case '8':localstr= DELAPAN+" " +RIBU;break;
case '9':localstr= SEMBILAN+" " +RIBU;break;
}
return localstr;
}
private String processDetail3(char charAt, int lenremain, String remainStr)
{
// System.out.println("Detail 3 ="+ lenremain + "=" + remainStr + " char "+charAt );
String localstr="";
switch (charAt)
{
case '0': //localstr= NOL;
break;
case '1':localstr= SE___+"" +RATUS;
//localstr= SATU;
break;
case '2':localstr= DUA+" " +RATUS;break;
case '3':localstr= TIGA+" " +RATUS;break;
case '4':localstr= EMPAT+" " +RATUS;break;
case '5':localstr= LIMA+" " +RATUS;break;
case '6':localstr= ENAM+" " +RATUS;break;
case '7':localstr= TUJUH+" " +RATUS;break;
case '8':localstr= DELAPAN+" " +RATUS;break;
case '9':localstr= SEMBILAN+" " +RATUS;break;
}
return localstr;
}
private String processDetail2(char charAt, int lenremain, String remainStr)
{
// System.out.println("Detail 2 ="+ lenremain + "=" + remainStr + " char "+charAt );
String localstr="";
switch (charAt)
{
case '0': //localstr= NOL;
break;
case '1':
anomalisebelas=true;
localstr= processDetailBelasan(charAt+remainStr);
//localstr= SATU;
break;
case '2':localstr= DUA+" " +PULUH;break;
case '3':localstr= TIGA+" " +PULUH;break;
case '4':localstr= EMPAT+" " +PULUH;break;
case '5':localstr= LIMA+" " +PULUH;break;
case '6':localstr= ENAM+" " +PULUH;break;
case '7':localstr= TUJUH+" " +PULUH;break;
case '8':localstr= DELAPAN+" " +PULUH;break;
case '9':localstr= SEMBILAN+" " +PULUH;break;
}
return localstr;
}
private String processDetail1(char charAt, int lenremain, String remainStr)
{
// System.out.println("Detail 1 ="+ lenremain + "=" + remainStr + " char "+charAt );
String localstr="";
if (anomalisebelas)
return localstr;
switch (charAt)
{
case '0':
if (getLen()>1)
{}
else
localstr= NOL;
break;
case '1':localstr= SATU;break;
case '2':localstr= DUA;break;
case '3':localstr= TIGA;break;
case '4':localstr= EMPAT;break;
case '5':localstr= LIMA;break;
case '6':localstr= ENAM;break;
case '7':localstr= TUJUH;break;
case '8':localstr= DELAPAN;break;
case '9':localstr= SEMBILAN;break;
}
return localstr;
}
private void processDetail(String origin)
{ StringBuilder sb= new StringBuilder();
String remainStr=origin;
for (int i=0; i< getLen();i++)
{
remainStr= origin.substring(i+1, getLen());
System.out.println(remainStr);
int lenremain= getLen()- (i+1);
System.out.println(lenremain + "=" + remainStr + " char "+origin.charAt(i) );
switch (lenremain)
{
case 0:
sb.append( processDetail1(origin.charAt(i),lenremain,remainStr));
break;
case 1:
sb.append( processDetail2(origin.charAt(i),lenremain,remainStr));
break;
case 2:
sb.append( processDetail3(origin.charAt(i),lenremain,remainStr));
break;
case 3:
// if (puluhanribu==true)
if (anomalisebelasribu)
sb.append(" "+ RIBU);
if (belasanjuta||ratusanribu||puluhanribu)
sb.append(" "+ RIBU);
if (anomalisebelasribu==false)
sb.append( processDetail4(origin.charAt(i),lenremain,remainStr));
break;
case 4:
sb.append( processDetail5(origin.charAt(i),lenremain,remainStr));
break;
case 5:
sb.append( processDetail6(origin.charAt(i),lenremain,remainStr));
break;
case 6:
sb.append( processDetail7(origin.charAt(i),lenremain,remainStr));
break;
case 7:
sb.append( processDetail8(origin.charAt(i),lenremain,remainStr));
default:
break;
}
sb.append(" ");
}
sb.append(" "+RUPIAH);
setOutput(sb.toString());
checkSpace();
}
private void checkSpace()
{
if (getInput().length()>4)
{
String [] selection= getOutput().split(" ");
int isribu=0;
for (int j=0;j<selection.length;j++)
{ if (selection[j].equals("ribu"))
isribu++;
}
boolean bisribu=false;
if (isribu>1)
{ StringBuilder s = new StringBuilder();
for (int j=0;j<selection.length;j++)
{
if ((bisribu==false) && selection[j].equals("ribu"))
//s.append(selection[j]);
{}
else
s.append(selection[j]);
if (selection[j].equals("ribu"))
bisribu=true;
s.append(" ");
}
setOutput(s.toString());
}
}
for (int i=0; i<8;i ++)
{ String c= getOutput();
setOutput(c.replaceAll(" ", " "));
}
for (int i=0; i<8;i ++)
{ String c= getOutput();
setOutput(c.replaceAll("se ", "se"));
}
}
public void process()
{
String debug="";
String begin= getInput().replaceAll(" -+.,Rp", "");
begin=begin.replace(" ", "");
//System.out.println(begin);
if (isNumeric(begin)==false)
setOutput("");
else
{
setLen(begin.length());
processDetail(begin);
}
}
/**
* @return the input
*/
public String getInput() {
return input;
}
/**
* @param input the input to set
*/
public void setInput(String input) {
this.input = input;
}
/**
* @return the len
*/
public int getLen() {
return len;
}
/**
* @param len the len to set
*/
public void setLen(int len) {
this.len = len;
}
/**
* @return the output
*/
public String getOutput() {
return output;
}
/**
* @param output the output to set
*/
public void setOutput(String output) {
this.output = output;
}
/**
* @return the selectDot
*/
public int getSelectDot() {
return selectDot;
}
/**
* @param selectDot the selectDot to set
*/
public void setSelectDot(int selectDot) {
this.selectDot = selectDot;
}
}
AmttoText amt= new AmttoText("46");
for (int i=900000;i<900001 ;i++)
{ System.out.println( i+ "-BEGIN-");
amt= new AmttoText(String.valueOf(i));
amt.process();
System.out.println( i+ "-END -" + amt.getOutput());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment