Programming Challenge Numbers to Text?

Stephen Smith scsmith1451 at totacc.com
Tue May 2 07:18:39 MDT 2006


Here is a simple pair of functions that will do the trick.  This is in 
Oracle's PL/SQL but should be readily readable by most programmers.  The 
VARCHAR2 data type is simply a string.  The || operator is the 
concatenation operator, and text strings are enclosed in '.

The first function, Get_NumberText, is used to retrieve a text string 
equivalent of the base numbers from a database table.  An array could do 
the same thing.  The second function, NumberToText, is recursive to 
build the final text string from the given number.  This could be more 
eligant with a bit more programming, however, this was quick and easy 
though limited in scope.

FUNCTION Get_NumberText(Value NUMBER) RETURN VARCHAR2 IS
    CURSOR cL(ThisNumber NUMBER) IS
      SELECT Text
      FROM   NumberTranslation
      WHERE  Value = ThisNumber;

    text     VARCHAR2(100);
  BEGIN
    OPEN cL(Value);
    FETCH cL INTO Text;
    CLOSE cL;

    RETURN text;
  END;

  FUNCTION NumberToText(Value NUMBER) RETURN VARCHAR2 IS
    tNum      INTEGER;
    tRem      INTEGER;
    tVal      INTEGER;
    hund      INTEGER:=100;
    thou      INTEGER:=1000;
    mill      INTEGER:=1000000;
    bill      INTEGER:=1000000000;
    trill     INTEGER:=1000000000000;
    text      VARCHAR2(200);
  BEGIN
    IF (Value < 0) THEN  -- Handles negative values
      text := 'MINUS '||NumberToText(ABS(Value));
    ELSE
      tNum := Value;
      IF (tNum = 0) THEN
        text := Get_NumberText(tNum);
      ELSE
        text := '';  -- Instantiate an empty string
        tVal := FLOOR(tNum/trill);
        tRem := MOD(tNum,trill);
        IF (tVal > 0) THEN
          text := NumberToText(tVal)||' '||Get_NumberText(trill);
          IF (tRem > 0) THEN
            text := text||' '||NumberToText(tRem);
          END IF;
        ELSE
          tVal := FLOOR(tNum/bill);
          tRem := MOD(tNum,bill);
          IF (tVal > 0) THEN
            text := NumberToText(tVal)||' '||Get_NumberText(bill);
            IF (tRem > 0) THEN
              text := text||' '||NumberToText(tRem);
            END IF;
          ELSE
            tVal := FLOOR(tNum/mill);
            tRem := MOD(tNum,mill);
            IF (tVal > 0) THEN
              text := NumberToText(tVal)||' '||Get_NumberText(mill);
              IF (tRem > 0) THEN
                text := text||' '||NumberToText(tRem);
              END IF;
            ELSE
              tVal := FLOOR(tNum/thou);
              tRem := MOD(tNum,thou);
              IF (tVal > 0) THEN
                text := NumberToText(tVal)||' '||Get_NumberText(thou);
                IF (tRem > 0) THEN
                  text := text||' '||NumberToText(tRem);
                END IF;
              ELSE
                tVal := FLOOR(tNum/hund);
                tRem := MOD(tNum,hund);
                IF (tVal > 0) THEN
                  text := NumberToText(tVal)||' '||Get_NumberText(hund);
                  IF (tRem > 0) THEN
                    text := text||' '||NumberToText(tRem);
                  END IF;
                ELSE
                  IF (tNum > 19) THEN
                    tVal := FLOOR(tNum/10)*10;
                    tRem := MOD(tNum,10);
                    text := Get_NumberText(tVal);
                    IF (tRem > 0) THEN
                      text := text||'-'||NumberToText(tRem);
                    END IF;
                  ELSE
                    text := Get_NumberText(tNum);
                  END IF;
                END IF;
              END IF;
            END IF;
          END IF;
        END IF;
      END IF;
    END IF;

    RETURN text;
  END;


These are the values I have in my table.  It can be extended to any 
upper limit you choose.

     VALUE TEXT
---------- ------------------------------
         0 ZERO
         1 ONE
         2 TWO
         3 THREE
         4 FOUR
         5 FIVE
         6 SIX
         7 SEVEN
         8 EIGHT
         9 NINE
        10 TEN
        11 ELEVEN
        12 TWELVE
        13 THIRTEEN
        14 FOURTEEN
        15 FIFTEEN
        16 SIXTEEN
        17 SEVENTEEN
        18 EIGHTTEEN
        19 NINETEEN
        20 TWENTY
        30 THIRTY
        40 FORTY
        50 FIFTY
        60 SIXTY
        70 SEVENTY
        80 EIGHTY
        90 NINETY
       100 HUNDRED
      1000 THOUSAND
   1000000 MILLION
1000000000 BILLION
1.0000E+12 TRILLION


Enjoy.



More information about the PLUG mailing list