//international number system
static String point = "point";
static String h = "hundred";
static String th = "thousand";
static String m = "million";
static String b = "billion";
static String
_0 = "",
_1 = "one",
_2 = "two",
_3 = "three",
_4 = "four",""
_5 = "five",
_6 = "six",
_7 = "seven",
_8 = "eight",
_9 = "nine";
static String
_00 = "",
_10 = "ten",
_20 = "twenty",
_30 = "thirty",
_40 = "forty",
_50 = "fifty",
_60 = "sixty",
_70 = "seventy",
_80 = "eighty",
_90 = "ninety";
static String
_11 = "eleven",
_12 = "twelve",
_13 = "thirteen",
_14 = "forteen",
_15 = "fifteen",
_16 = "sixteen",
_17 = "seventeen",
_18 = "eighteen",
_19 = "nineteen";
static String
_000 = "",
_100 = _1 + " " + h,
_200 = _2 + " " + h,
_300 = _3 + " " + h,
_400 = _4 + " " + h,
_500 = _5 + " " + h,
_600 = _6 + " " + h,
_700 = _7 + " " + h,
_800 = _8 + " " + h,
_900 = _9 + " " + h;
static String[][][] numberNames = new String[][][]{
{
{_0, _1, _2, _3, _4, _5, _6, _7, _8, _9},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90},
{_000, _100, _200, _300, _400, _500, _600, _700, _800, _900}
},
{
{_0, _1+" "+th, _2+" "+th, _3+" "+th, _4+" "+th, _5+" "+th,
_6+" "+th, _7+" "+th, _8+" "+th, _9+" "+th},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90},
{_000, _100, _200, _300, _400, _500, _600, _700, _800, _900}
},
{
{_0, _1+" "+m, _2+" "+m, _3+" "+m, _4+" "+m, _5+" "+m,
_6+" "+m, _7+" "+m, _8+" "+m, _9+" "+m},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90},
{_000, _100, _200, _300, _400, _500, _600, _700, _800, _900}
},
{
{_0, _1+" "+b, _2+" "+b, _3+" "+b, _4+" "+b, _5+" "+b,
_6+" "+b, _7+" "+b, _8+" "+b, _9+" "+b},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90},
{_000, _100, _200, _300, _400, _500, _600, _700, _800, _900}
}
};
//indian number system
static String l = "lakh";
static String c = "crore";
static String a = "arab";
static String[][][] indianNumberNames = new String[][][]{
{
{_0, _1, _2, _3, _4, _5, _6, _7, _8, _9},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90},
{_000, _100, _200, _300, _400, _500, _600, _700, _800, _900}
},
{
{_0, _1+" "+th, _2+" "+th, _3+" "+th, _4+" "+th, _5+" "+th,
_6+" "+th, _7+" "+th, _8+" "+th, _9+" "+th},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90}
},
{
{_0, _1+" "+l, _2+" "+l, _3+" "+l, _4+" "+l, _5+" "+l,
_6+" "+l, _7+" "+l, _8+" "+l, _9+" "+l},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90}
},
{
{_0, _1+" "+c, _2+" "+c, _3+" "+c, _4+" "+c, _5+" "+c,
_6+" "+c, _7+" "+c, _8+" "+c, _9+" "+c},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90}
},
{
{_0, _1+" "+a, _2+" "+a, _3+" "+a, _4+" "+a, _5+" "+a,
_6+" "+a, _7+" "+a, _8+" "+a, _9+" "+a},
{_00, _10, _20, _30, _40, _50, _60, _70, _80, _90}
}
};
//the grouping of numbers,
//such as 1 000 000 000 where each group contains 3 digits,
//or 1 00 00 00 000 for indian number system,
//where each group contains 2 digits except the first group which has 3.
//may differ from one number system to another.
static int place_difference = 3;
//current number system
static int curNumSys = INTERNATIONAL_NUMBER_SYSTEM;
public static String convertToNumberName(String number){
String[] split = number.split("\\.");
String num1, num2;
num1 = split[0];
num2 = split.length > 1 ? split[1] : "";
int place_counter = 0;
int step = 0;
Stack<String> numberNameBuilder = new Stack<>();
StringBuilder builder = new StringBuilder();
builder.append(num1);
num1 = (builder.reverse()).toString();
builder.setLength(0);
int counter = -1;
for(int i = 0; i < num1.length(); i++){
int c = Integer.parseInt(num1.charAt(i) + "");
counter++;
place_counter = counter % place_difference;
if (place_counter == 0){
step++;
//specific number system component, this is for indian number system
//find a generic approach at a later time
// if (step == 2){
// counter = 0;
// place_difference = 2;
// }
}
String name = getDataSet()[step - 1][place_counter][c];
numberNameBuilder.push(name);
}
while(numberNameBuilder.hasNext()){
builder.append(numberNameBuilder.pop() + " ");
}
String str = builder.toString();
str = refine(str);
builder.setLength(0);
builder.append(num2);
num2 = builder.reverse().toString();
builder.setLength(0);
//decimal conversion here
for(int i = 0; i < num2.length(); i++){
int c = Integer.parseInt(num2.charAt(i) + "");
numberNameBuilder.push(getDataSet()[0][0][c]);
}
while(numberNameBuilder.hasNext()){
builder.append(numberNameBuilder.pop() + " ");
}
str += point + " ";
str += builder.toString();
return str;
}
private static String refine(String str){
str = str.replace(_10 + " " + _1, _11);
str = str.replace(_10 + " " + _2, _12);
str = str.replace(_10 + " " + _3, _13);
str = str.replace(_10 + " " + _4, _14);
str = str.replace(_10 + " " + _5, _15);
str = str.replace(_10 + " " + _6, _16);
str = str.replace(_10 + " " + _7, _17);
str = str.replace(_10 + " " + _8, _18);
str = str.replace(_10 + " " + _9, _19);
return str;
}
private static String[][][] getDataSet(){
switch(curNumSys){
case INDIAN_NUMBER_SYSTEM :
return indianNumberNames;
case INTERNATIONAL_NUMBER_SYSTEM :
return numberNames;
}
return numberNames;
}
Just simply call the convertToNumberName() function and give the numbers as string,to change from international to Indian, simply uncomment the commented lines in the function mentioned above, to add more number systems, more data sets and minor tweaking in the function will be required.
The stack mentioned here is just a simple LIFO structure and with ordinary stack functions, one could use their own custom implementation of the stack here or just import from Java.
The limit can be expanded further, for example, international number system only has capacity to convert up-to 999 billions, however, the data set can be expanded further to add trillions and quadrillions and more, it can go as far as the dataset that is the 3d array in code above allows it to.