Advertisement
minafaw3

EnglishNumberToWords

Jan 16th, 2016
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2.  
  3. public class EnglishNumberToWords {
  4.  
  5. private static final String[] tensNames = {
  6. "",
  7. " ten",
  8. " twenty",
  9. " thirty",
  10. " forty",
  11. " fifty",
  12. " sixty",
  13. " seventy",
  14. " eighty",
  15. " ninety"
  16. };
  17.  
  18. private static final String[] numNames = {
  19. "",
  20. " one",
  21. " two",
  22. " three",
  23. " four",
  24. " five",
  25. " six",
  26. " seven",
  27. " eight",
  28. " nine",
  29. " ten",
  30. " eleven",
  31. " twelve",
  32. " thirteen",
  33. " fourteen",
  34. " fifteen",
  35. " sixteen",
  36. " seventeen",
  37. " eighteen",
  38. " nineteen"
  39. };
  40.  
  41. private EnglishNumberToWords() {}
  42.  
  43. private static String convertLessThanOneThousand(int number) {
  44. String soFar;
  45.  
  46. if (number % 100 < 20){
  47. soFar = numNames[number % 100];
  48. number /= 100;
  49. }
  50. else {
  51. soFar = numNames[number % 10];
  52. number /= 10;
  53.  
  54. soFar = tensNames[number % 10] + soFar;
  55. number /= 10;
  56. }
  57. if (number == 0) return soFar;
  58. return numNames[number] + " hundred" + soFar;
  59. }
  60.  
  61.  
  62. public static String convert(long number) {
  63. // 0 to 999 999 999 999
  64. if (number == 0) { return "zero"; }
  65.  
  66. String snumber = Long.toString(number);
  67.  
  68. // pad with "0"
  69. String mask = "000000000000";
  70. DecimalFormat df = new DecimalFormat(mask);
  71. snumber = df.format(number);
  72.  
  73. // XXXnnnnnnnnn
  74. int billions = Integer.parseInt(snumber.substring(0,3));
  75. // nnnXXXnnnnnn
  76. int millions = Integer.parseInt(snumber.substring(3,6));
  77. // nnnnnnXXXnnn
  78. int hundredThousands = Integer.parseInt(snumber.substring(6,9));
  79. // nnnnnnnnnXXX
  80. int thousands = Integer.parseInt(snumber.substring(9,12));
  81.  
  82. String tradBillions;
  83. switch (billions) {
  84. case 0:
  85. tradBillions = "";
  86. break;
  87. case 1 :
  88. tradBillions = convertLessThanOneThousand(billions)
  89. + " billion ";
  90. break;
  91. default :
  92. tradBillions = convertLessThanOneThousand(billions)
  93. + " billion ";
  94. }
  95. String result = tradBillions;
  96.  
  97. String tradMillions;
  98. switch (millions) {
  99. case 0:
  100. tradMillions = "";
  101. break;
  102. case 1 :
  103. tradMillions = convertLessThanOneThousand(millions)
  104. + " million ";
  105. break;
  106. default :
  107. tradMillions = convertLessThanOneThousand(millions)
  108. + " million ";
  109. }
  110. result = result + tradMillions;
  111.  
  112. String tradHundredThousands;
  113. switch (hundredThousands) {
  114. case 0:
  115. tradHundredThousands = "";
  116. break;
  117. case 1 :
  118. tradHundredThousands = "one thousand ";
  119. break;
  120. default :
  121. tradHundredThousands = convertLessThanOneThousand(hundredThousands)
  122. + " thousand ";
  123. }
  124. result = result + tradHundredThousands;
  125.  
  126. String tradThousand;
  127. tradThousand = convertLessThanOneThousand(thousands);
  128. result = result + tradThousand;
  129.  
  130. // remove extra spaces!
  131. return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
  132. }
  133.  
  134. /**
  135. * testing
  136. * @param args
  137. */
  138. public static void main(String[] args) {
  139. System.out.println("*** " + EnglishNumberToWords.convert(0));
  140. System.out.println("*** " + EnglishNumberToWords.convert(1));
  141. System.out.println("*** " + EnglishNumberToWords.convert(16));
  142. System.out.println("*** " + EnglishNumberToWords.convert(100));
  143. System.out.println("*** " + EnglishNumberToWords.convert(118));
  144. System.out.println("*** " + EnglishNumberToWords.convert(200));
  145. System.out.println("*** " + EnglishNumberToWords.convert(219));
  146. System.out.println("*** " + EnglishNumberToWords.convert(800));
  147. System.out.println("*** " + EnglishNumberToWords.convert(801));
  148. System.out.println("*** " + EnglishNumberToWords.convert(1316));
  149. System.out.println("*** " + EnglishNumberToWords.convert(1000000));
  150. System.out.println("*** " + EnglishNumberToWords.convert(2000000));
  151. System.out.println("*** " + EnglishNumberToWords.convert(3000200));
  152. System.out.println("*** " + EnglishNumberToWords.convert(700000));
  153. System.out.println("*** " + EnglishNumberToWords.convert(9000000));
  154. System.out.println("*** " + EnglishNumberToWords.convert(9001000));
  155. System.out.println("*** " + EnglishNumberToWords.convert(123456789));
  156. System.out.println("*** " + EnglishNumberToWords.convert(2147483647));
  157. System.out.println("*** " + EnglishNumberToWords.convert(3000000010L));
  158.  
  159. /*
  160. *** zero
  161. *** one
  162. *** sixteen
  163. *** one hundred
  164. *** one hundred eighteen
  165. *** two hundred
  166. *** two hundred nineteen
  167. *** eight hundred
  168. *** eight hundred one
  169. *** one thousand three hundred sixteen
  170. *** one million
  171. *** two millions
  172. *** three millions two hundred
  173. *** seven hundred thousand
  174. *** nine millions
  175. *** nine millions one thousand
  176. *** one hundred twenty three millions four hundred
  177. ** fifty six thousand seven hundred eighty nine
  178. *** two billion one hundred forty seven millions
  179. ** four hundred eighty three thousand six hundred forty seven
  180. *** three billion ten
  181. **/
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement