Convert Number to Words
Kindly Help me Explain this Line of Codes.
Line by Line if possible.
It was given to me by my friend.
Hoping for Some Help.
Thank you very much.
import java.util.Scanner;
public class WordNumber{
public static void main(String[]args){
for(int i = 0; i < 20; i++){
Scanner input = new Scanner(System.in);
System.out.print("Enter a Non Floating Point Number: ");
long x = input.nextLong();
System.out.println(makeWord(x)); //Please Explain
}//end for
}//end main
//Please Explain
private static final String[] numbers = {
"zero","one","two","three","four","five","six",
"seven","eight","nine","ten","eleven","twelve",
"thirteen","fourteen","fifteen","sixteen","seventeen",
"eighteen","nineteen"};
private static final String[] tens = {
"","teen","twenty","thirty","fourty","fifty",
"sixty","seventy","eighty","ninety"};
//Please Explain
public static String makeWord(long number){
String s = "";
if(number<0){
s="negative ";
number*=-1;
}
s+=toWord(number);
s = s.replace("-zero","");
return s;
}//end of makeWord
//Please Explain
private static String toWord(long number){
if(number>=1000000000){
return toWord(number/1000000000)+"-billion-"+toWord(number%1000000000);
}
else if(number>=1000000){
return toWord(number/1000000)+"-million-"+toWord(number%1000000);
}
else if(number>=1000){
return "" +toWord(number/1000)+"-thousand-"+toWord(number%1000);
}
else if(number>=100){
return numbers[(int)number/100] +"-hundred-"+toWord((long)number%(long)100);//Please Explain
}
else if(number>=20){
return tens[(int)number/10] + "-" + toWord(number%10);
}
else if(number>18){
return numbers[(int)number%10]+"teen";
}
else{
return numbers[(int)number];
}
}//end of toWord
}//end class