fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. private static final String[] NUM_NAMES = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
  11.  
  12. private static final String[] TEEN_NAMES = { "", "eleven", "twelve", "thirteen", "fourteen",
  13. "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
  14.  
  15. private static final String[] TENS_NAMES = { "", "ten", "twenty", "thirty", "forty", "fifty",
  16. "sixty", "seventy", "eighty", "ninety" };
  17.  
  18. private static int getRemainder(int dividend, int i, int k) {
  19. return ((dividend % (int) Math.pow(10, (i))) - (dividend % (int) Math.pow(10, (k))))
  20. / (int) Math.pow(10, (k));
  21. }
  22.  
  23. /*
  24. * The method convertNumToText is capable of evaluating numbers up to 999,999,999 and
  25. * translating them into their readable English equivalent.
  26. */
  27. private static String convertNumToText(int num) {
  28. // Uses StringBuilder to append values instead of String concatenation
  29. StringBuilder str = new StringBuilder();
  30. // Calculates the logarithmic value for the number entered
  31. int length = (int) (Math.log10(num));
  32. // Placeholder variable for the result of modulus calculations
  33. int remainder;
  34.  
  35. // Catches if the number being parsed is 0 and returns "zero"
  36. if (num == 0) {
  37. return "zero";
  38. }
  39.  
  40. /*
  41. * This For Loop isolates every digit of the number entered,
  42. * and appends the English value into a string
  43. */
  44. for (int i = length; i >= 0; i--) {
  45.  
  46. /*
  47. * Isolates the highest-level digit using modulus
  48. * e.g. : ((4321%10^4) - (4321%10^3) / 10^3)
  49. * = ((4000) - (321)) / 1000
  50. * = 4
  51. */
  52. remainder = getRemainder(num, i + 1, i);
  53.  
  54. /*
  55. * IF the digits place is tens AND the value is not 10 (11-19) then apply the nomenclature of
  56. * TEEN_NAMES
  57. *
  58. * ELSE if the digits place is tens and 10,20,30... then apply the nomenclature TEN_NAMES
  59. *
  60. * ELSE if no special nomenclature is required, just apply regular nomenclature NUM_NAMES
  61. */
  62. if ((i == 1 || i == 4 || i == 7) && (remainder == 1 && ((10 + getRemainder(num, i, i - 1) != 10)))) {
  63. str.append(TEEN_NAMES[getRemainder(num, i, i - 1)]);
  64. i--; // Skip the ones value, since the TEENS naming consumes both tens and ones
  65. } else if (i == 1 || i == 4 || i == 7) {
  66. str.append(TENS_NAMES[remainder]);
  67. if (getRemainder(num, i, i - 1) != 0 && getRemainder(num, i+1, i) != 0) {
  68. str.append("-"); // If the value of the ones is not zero, then append a dash
  69. }
  70. } else {
  71. str.append(NUM_NAMES[remainder]);
  72. }
  73.  
  74. /*
  75. * If the digit places is hundreds, then apply suffix of "hundred"
  76. *
  77. * This is not a part of the thousand/million statements because those only have one
  78. * placeholder and hundred has multiple
  79. */
  80. if (remainder > 0 && (i == 2 || i == 5 || i == 8)) {
  81. str.append(" hundred ");
  82. }
  83.  
  84. /*
  85. * IF the digits place is thousands, then apply suffix "thousand"
  86. *
  87. * ELSE IF the digits place is millions, then apply suffix "million"
  88. *
  89. * New line added for readability.
  90. */
  91. if (i == 3) {
  92. str.append(" thousand,\n");
  93. } else if (i == 6) {
  94. str.append(" million,\n");
  95. }
  96. // For debugging purposes
  97. //System.out.println(remainder + " " + i);
  98. }
  99. // Converts the StringBuilder str into a string and returns the value
  100. return str.toString();
  101. }
  102. public static void main (String[] args) throws java.lang.Exception
  103. {
  104.  
  105. System.out.println(convertNumToText(231242));
  106. }
  107. }
Success #stdin #stdout 0.1s 320576KB
stdin
stdout
two hundred thirty-one thousand,
two hundred forty-two