Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vinaypuranik/4b7867856ea75c1deede4630f770899d to your computer and use it in GitHub Desktop.
Save vinaypuranik/4b7867856ea75c1deede4630f770899d to your computer and use it in GitHub Desktop.
Checkwriter for Number to Euros represenatation.
public class CheckWriter {
public static String BASIC_MONEYTARY_UNIT = " Euros";
public static String MONEYTARY_UNIT = " cents";
public static int MILLIONS = 1000000;
public static int THOUSANDS = 1000;
public static void main(String[] args) {
System.out.println(numberToWords("999999.00"));
System.out.println(numberToWords("22930.10"));
System.out.println(numberToWords("1999.01"));
System.out.println(numberToWords("199.20"));
System.out.println(numberToWords("0"));
System.out.println(numberToWords("10"));
System.out.println(numberToWords("1200"));
System.out.println(numberToWords("112.10"));
System.out.println(numberToWords("874327.9"));
System.out.println(numberToWords("978"));
System.out.println(numberToWords("1.01"));
}
public static String numberToWords(String number) {
int indexOfDecimalPoint = number.indexOf('.');
int numberBeforeDecimalPoint;
int numberAfterDecimalPoint;
if(indexOfDecimalPoint != -1) {
numberBeforeDecimalPoint = Integer.parseInt(number.substring(0, indexOfDecimalPoint));
numberAfterDecimalPoint = Integer.parseInt(number.substring(indexOfDecimalPoint+1, number.length()));
} else {
numberBeforeDecimalPoint = Integer.parseInt(number);
numberAfterDecimalPoint = 0;
}
int million = numberBeforeDecimalPoint/MILLIONS;
int thousand = (numberBeforeDecimalPoint - million * MILLIONS)/THOUSANDS;
int rest = numberBeforeDecimalPoint - million * MILLIONS - thousand * THOUSANDS ;
String euros = "";
String cents = "";
if(million != 0) {
if(!euros.isEmpty())
euros += " ";
euros += three(million) + " Million";
}
if(thousand != 0) {
if(!euros.isEmpty())
euros+= " ";
euros +=three(thousand) + " Thousand";
}
if(rest != 0) {
if( !euros.isEmpty())
euros+= " ";
euros += three(rest);
}
if(numberAfterDecimalPoint != 0) {
if(!cents.isEmpty())
cents+= " ";
cents += three(numberAfterDecimalPoint);
}
if(numberBeforeDecimalPoint == 0) {
euros += "Zero";
}
if(numberAfterDecimalPoint == 0) {
cents += "Zero";
}
return euros + BASIC_MONEYTARY_UNIT + " and " + cents + MONEYTARY_UNIT;
}
public static String one(int num) {
switch(num) {
case 1: return "One";
case 2: return "Two";
case 3: return "Three";
case 4: return "Four";
case 5: return "Five";
case 6: return "Six";
case 7: return "Seven";
case 8: return "Eight";
case 9: return "Nine";
}
return "";
}
public static String twoLessThan20(int num) {
switch(num) {
case 10: return "Ten";
case 11: return "Eleven";
case 12: return "Twelve";
case 13: return "Thirteen";
case 14: return "Fourteen";
case 15: return "Fifteen";
case 16: return "Sixteen";
case 17: return "Seventeen";
case 18: return "Eighteen";
case 19: return "Nineteen";
}
return "";
}
public static String ten(int num) {
switch(num) {
case 2: return "Twenty";
case 3: return "Thirty";
case 4: return "Forty";
case 5: return "Fifty";
case 6: return "Sixty";
case 7: return "Seventy";
case 8: return "Eighty";
case 9: return "Ninety";
}
return "";
}
public static String three(int num) {
int hundred = num / 100;
int rest = num - hundred * 100;
String res = "";
if (hundred*rest != 0)
res = one(hundred) + " Hundred " + two(rest);
else if ((hundred == 0) && (rest != 0))
res = two(rest);
else if ((hundred != 0) && (rest == 0))
res = one(hundred) + " Hundred";
return res;
}
public static String two(int num) {
if (num == 0)
return "";
else if (num < 10)
return one(num);
else if (num < 20)
return twoLessThan20(num);
else {
int tenner = num / 10;
int rest = num - tenner * 10;
if (rest != 0)
return ten(tenner) + " " + one(rest);
else
return ten(tenner);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment