Home

16.12.11

Convert numbers in to words (Java)

Convert numbers in to words (Java)

Short and sweet?

http://www.daniweb.com/software-development/java/threads/67369/page2

  1. /** Holds the number 1-19, dual purpose for special cases (teens) **/
  2. private static final String[] UNITS = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
  3. /** Holds the tens places **/
  4. private static final String[] TENS = {"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninty"};
  5. /** Covers max value of Long **/
  6. private static final String[] THOUSANDS = {"", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion"};
  7.  
  8. /**
  9.   * Represents a number in words (seven hundred thirty four, two hundred and seven, etc...)
  10.   *
  11.   * The largest number this will accept is
  12.   *
    999,999,999,999,999,999,999
    but that's okay becasue the largest
  13.   * value of Long is
    9,223,372,036,854,775,807
    . The smallest number
  14.   * is
    -9,223,372,036,854,775,807
    (Long.MIN_VALUE +1) due to a
  15.   * limitation of Java.
  16.   * @param number between Long.MIN_VALUE and Long.MAX_VALUE
  17.   * @return the number writen in words
  18.   */
  19. public static String numberInWords(long number) {
  20. StringBuilder sb = new StringBuilder();
  21. // Zero is an easy one, but not technically a number :P
  22. if (number == 0) {
  23. return "zero";
  24. }
  25. // Negative numbers are easy too
  26. if (number < 0) {
  27. sb.append("negative ");
  28. number = Math.abs(number);
  29. }
  30.  
  31. // Log keeps track of which Thousands place we're in
  32. long log = 1000000000000000000L, sub = number;
  33. int i = 7;
  34. do {
  35. // This is the 1-999 subset of the current thousand's place
  36. sub = number / log;
  37. // Cut down number for the next loop
  38. number = number % log;
  39. // Cut down log for the next loop
  40. log = log / 1000;
  41. i--; // tracks the big number place
  42. if (sub != 0) {
  43. // If this thousandths place is not 0 (that's okay, just skip)
  44. // tack it on
  45. sb.append(hundredsInWords((int) sub));
  46. sb.append(" ");
  47. sb.append(THOUSANDS[i]);
  48. if (number != 0) {
  49. sb.append(" ");
  50. }
  51. }
  52. } while (number != 0);
  53.  
  54. return sb.toString();
  55. }
  56.  
  57. /**
  58.   * Converts a number into hundreds.
  59.   *
  60.   * The basic unit of the American numbering system is "hundreds". Thus
  61.   * 1,024 = (one thousand) twenty four
  62.   * 1,048,576 = (one million) (fourty eight thousand) (five hundred seventy six)
  63.   * so there's no need to break it down further than that.
  64.   * @param n between 1 and 999
  65.   * @return
  66.   */
  67. private static String hundredsInWords(int n) {
  68. if (n < 1 || n > 999) {
  69. throw new AssertionError(n); // Use assersion errors in private methods only!
  70. }
  71. StringBuilder sb = new StringBuilder();
  72.  
  73. // Handle the "hundred", with a special provision for x01-x09
  74. if (n > 99) {
  75. sb.append(UNITS[(n / 100) - 1]);
  76. sb.append(" hundred");
  77. n = n % 100;
  78. if (n != 0) {
  79. sb.append(" ");
  80. }
  81. if(n < 10){
  82. sb.append("and ");
  83. }
  84. }
  85.  
  86. // Handle the special cases and the tens place at the same time.
  87. if (n > 19) {
  88. sb.append(TENS[(n / 10) - 1]);
  89. n = n % 10;
  90. if (n != 0) {
  91. sb.append(" ");
  92. }
  93. }
  94.  
  95. // This is the ones place
  96. if (n > 0) {
  97. sb.append(UNITS[n - 1]);
  98. }
  99. return sb.toString();
  100. }

No comments:

Related Posts Plugin for WordPress, Blogger...