Problem of the Day
A new programming or logic puzzle every Mon-Fri

Integer Humanize

Today's problem you will hopefully find super useful. It will likely come in handy in some project for the future. The goal is to convert integers to their character/language equivalent. Your program should be able to do up to 999 thousand. For example:

9 => nine
245 = > two hundred fourty five
99999 => ninety nine thousand nine hundred

My examples use English but feel free to use whatever language you are most comfortable with.

Permalink: http://problemotd.com/problem/integer-humanize/

Comments:

  • JtMach - 9 years, 10 months ago

    JavaScript, I'm sure there's a more elegant solution. But this works.

    this.window.onload = function () {
      for (var intVal = 1; intVal <= 1000000; intVal++) {
        this.document.writeln(intVal + " => " + GetText(intVal.toString()) + "<br />");
      }
    };
    function GetText(numVal) {
      var result = "";
      var hundredsCount = Math.floor((numVal.length - 1) / 3) + 1;
      for (var hundredsIndex = 0; hundredsIndex < hundredsCount; hundredsIndex++) {
        if (hundredsIndex == (hundredsCount - 1)) {
          switch (hundredsIndex) {
            case 1:
              result = " thousand " + result;
              break;
            case 2:
              result = " million " + result;
              break;
            case 3:
              result = " billion " + result;
              break;
          }
    
          if (numVal.length % 3 === 0) {
            result = GetHundreds(numVal.substr(numVal.length - ((hundredsIndex * 3) + 3), 3)) + " " + result;
          }
          else {
            result = GetHundreds(numVal.substr(numVal.length - ((hundredsIndex * 3) + (numVal.length % 3)), numVal.length % 3)) + " " + result;
          }
        }
        else {
          result = GetHundreds(numVal.substr(numVal.length - ((hundredsIndex + 1) * 3), 3)) + result;
        }
      }
      return result;
    }
    function GetHundreds(numVal) {
      switch(numVal.length) {
        case 1:
          return GetSingle(numVal.charAt(numVal.length - 1));
        case 2:
          return GetTens(numVal.substr(numVal.length - 2, 2));
        case 3:
          if (numVal.charAt(numVal.length - 3) != "0") {
            return GetSingle(numVal.charAt(numVal.length - 3)) + " hundred " + GetTens(numVal.substr(numVal.length - 2, 2));
          }
          return GetSingle(numVal.charAt(numVal.length - 3)) + " " + GetTens(numVal.substr(numVal.length - 2, 2));
        default:
          return ' ';
      }
    }
    function GetSingle(numVal) {
      switch (numVal) {
        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";
        default:
          return "";
      }
    }
    function GetTens(numVal) {
      switch (numVal.charAt(0)) {
        case '0':
          return " " + GetSingle(numVal.charAt(1));
        case '1':
          switch (numVal.charAt(1)) {
            case '1':
              return "eleven";
            case '2':
              return "twelve";
            case '3':
              return "thirteen";
            case '4':
              return "fourteen";
            case '5':
              return "fifteen";
            case '6':
              return "sixteen";
            case '7':
              return "seventeen";
            case '8':
              return "eighteen";
            case '9':
              return "nineteen";
            default:
              return "ten";
          }
          break;
        case '2':
          return "twenty" + " " + GetSingle(numVal.charAt(1));
        case '3':
          return "thirty" + " " + GetSingle(numVal.charAt(1));
        case '4':
          return "fourty" + " " + GetSingle(numVal.charAt(1));
        case '5':
          return "fifty" + " " + GetSingle(numVal.charAt(1));
        case '6':
          return "sixty" + " " + GetSingle(numVal.charAt(1));
        case '7':
          return "seventy" + " " + GetSingle(numVal.charAt(1));
        case '8':
          return "eighty" + " " + GetSingle(numVal.charAt(1));
        case '9':
          return "ninety" + " " + GetSingle(numVal.charAt(1));
        default:
          return "";
      }
    }
    

    reply permalink

  • Driphter - 9 years, 8 months ago

    Clojure!

    (def- ones
      {"0" ""
       "1" "one"
       "2" "two"
       "3" "three"
       "4" "four"
       "5" "five"
       "6" "six"
       "7" "seven"
       "8" "eight"
       "9" "nine"})
    
    (def- teens
      {"11" "eleven"
       "12" "twelve"
       "13" "thirteen"
       "14" "fourteen"
       "15" "fifteen"
       "16" "sixteen"
       "17" "seventeen"
       "18" "eighteen"
       "19" "nineteen"})
    
    (def- tens
      {"1" "ten"
       "2" "twenty"
       "3" "thirty"
       "4" "fourty"
       "5" "fifty"
       "6" "sixty"
       "7" "seventy"
       "8" "eighty"
       "9" "ninety"})
    
    (def- digit-group
      {1 "thousand"
       2 "million"
       3 "billion"
       4 "trillion"
       5 "quadrillion"})
    
    (defn- numeral->english
      [s]
      (let [length (count s)]
        (cond
          (= 1 length) (ones s)
          (= 2 length) (let [d1 (subs s 1)
                             d2 (subs s 0 1)]
                         (cond
                           (= "0" d2) (numeral->english d1)
                           (= "0" d1) (tens d2)
                           (= "1" d2) (teens s)
                           :else (str (tens d2) "-" (ones d1))))
          (= 3 length) (let [d3 (subs s 0 1)]
                         (if (= "0" d3)
                           (numeral->english (subs s 1))
                           (str (ones d3) " hundred " (numeral->english (subs s 1)))))
          (< 3 length) (let [digits (rem length 3)
                             digits (if (zero? digits) 3 digits)
                             left (subs s 0 digits)
                               right (subs s digits)]
                         (if (= "000" left)
                           (numeral->english right)
                           (str (numeral->english left)
                                " " (digit-group (quot (dec length) 3)) " "
                                (numeral->english right)))))))
    
    (defn int->english
      [x]
      (if (zero? x)
        "zero"
        (numeral->english (str x))))
    

    reply permalink

Content curated by @MaxBurstein