Code Golf: Number to Words

Jason Z picture Jason Z · Nov 21, 2008 · Viewed 10.7k times · Source

The code golf series seem to be fairly popular. I ran across some code that converts a number to its word representation. Some examples would be (powers of 2 for programming fun):

  • 2 -> Two
  • 1024 -> One Thousand Twenty Four
  • 1048576 -> One Million Forty Eight Thousand Five Hundred Seventy Six

The algorithm my co-worker came up was almost two hundred lines long. Seems like there would be a more concise way to do it.

Current guidelines:

  • Submissions in any programming language welcome (I apologize to PhiLho for the initial lack of clarity on this one)
  • Max input of 2^64 (see following link for words, thanks mmeyers)
  • Short scale with English output preferred, but any algorithm is welcome. Just comment along with the programming language as to the method used.

Answer

Paulius picture Paulius · Nov 22, 2008

Ok, I think it's time for my own implementation in Windows BATCH script (should work on Windows 2000 or later).

Here is the code:

@echo off

set zero_to_nineteen=Zero One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen
set twenty_to_ninety=ignore ignore Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety
set big_numbers=ignore Thousand Million Billion Trillion Quadrillion Quintillion Sextillion Septillion Octillion Nonillion Decillion Undecillion Duodecillion Tredecillion Quattuordecillion Quindecillion Sexdecillion Septendecillion Octodecillion Novemdecillion Vigintillion
rem             10^0   10^3     10^6    10^9    10^12    10^15       10^18       10^21      10^24      10^27     10^30     10^33     10^36       10^39        10^42        10^45             10^48         10^51        10^54           10^57         10^60          10^63

call :parse_numbers %*

exit /B 0

:parse_numbers
    :parse_numbers_loop
        if "$%~1" == "$" goto parse_numbers_end
        call :parse_number %~1
        echo %~1 -^> %parse_number_result%
        shift
        goto parse_numbers_loop
    :parse_numbers_end
    exit /B 0

:parse_number
    call :get_sign %~1
    set number_sign=%get_sign_result%
    call :remove_groups %get_sign_result_number%
    call :trim_leading_zeros %remove_groups_result%
    set number=%trim_leading_zeros_result%
    if "$%number%" == "$0" (
        set parse_number_result=Zero
        exit /B 0
    )
    set counter=0
    set parse_number_result=
    :parse_number_loop
        set last_three=%number:~-3%
        set number=%number:~0,-3%
        call :parse_three %last_three%
        call :get_from %counter% %big_numbers%
        if "$%get_from_result%" == "$" (
            set parse_number_result=* ERR: the number is too big! Even wikipedia doesn't know how it's called!
            exit /B 0
        )
        if not "$%parse_three_result%" == "$Zero" (
            if %counter% == 0 (
                set parse_number_result=%parse_three_result%
            ) else (
                if not "$%parse_number_result%" == "$" (
                    set parse_number_result=%parse_three_result% %get_from_result% %parse_number_result%
                ) else (
                    set parse_number_result=%parse_three_result% %get_from_result%
                )
            )
        )
        set /A counter+=1
        if not "$%number%" == "$" goto parse_number_loop
    if "$%parse_number_result%" == "$" (
        set parse_number_result=Zero
        exit /B 0
    ) else if not "$%number_sign%" == "$" (
        set parse_number_result=%number_sign% %parse_number_result%
    )
    exit /B 0

:parse_three
    call :trim_leading_zeros %~1
    set three=%trim_leading_zeros_result%
    set /A three=%three% %% 1000
    set /A two=%three% %% 100
    call :parse_two %two%
    set parse_three_result=
    set /A digit=%three% / 100
    if not "$%digit%" == "$0" (
        call :get_from %digit% %zero_to_nineteen%
    )
    if not "$%digit%" == "$0" (
        if not "$%get_from_result%" == "$Zero" (
            set parse_three_result=%get_from_result% Hundred
        )
    )
    if "$%parse_two_result%" == "$Zero" (
        if "$%parse_three_result%" == "$" (
            set parse_three_result=Zero
        )
    ) else (
        if "$%parse_three_result%" == "$" (
            set parse_three_result=%parse_two_result%
        ) else (
            set parse_three_result=%parse_three_result% %parse_two_result%
        )
    )
    exit /B 0

:parse_two
    call :trim_leading_zeros %~1
    set two=%trim_leading_zeros_result%
    set /A two=%two% %% 100
    call :get_from %two% %zero_to_nineteen%
    if not "$%get_from_result%" == "$" (
        set parse_two_result=%get_from_result%
        goto parse_two_20_end
    )
    set /A digit=%two% %% 10
    call :get_from %digit% %zero_to_nineteen%
    set parse_two_result=%get_from_result%
    set /A digit=%two% / 10
    call :get_from %digit% %twenty_to_ninety%
    if not "$%parse_two_result%" == "$Zero" (
        set parse_two_result=%get_from_result% %parse_two_result%
    ) else (
        set parse_two_result=%get_from_result%
    )
    goto parse_two_20_end
    :parse_two_20_end
    exit /B 0

:get_from
    call :trim_leading_zeros %~1
    set idx=%trim_leading_zeros_result%
    set /A idx=0+%~1
    shift
    :get_from_loop
        if "$%idx%" == "$0" goto get_from_loop_end
        set /A idx-=1
        shift
        goto get_from_loop
    :get_from_loop_end
    set get_from_result=%~1
    exit /B 0

:trim_leading_zeros
    set str=%~1
    set trim_leading_zeros_result=
    :trim_leading_zeros_loop
        if not "$%str:~0,1%" == "$0" (
            set trim_leading_zeros_result=%trim_leading_zeros_result%%str%
            exit /B 0
        )
        set str=%str:~1%
        if not "$%str%" == "$" goto trim_leading_zeros_loop
    if "$%trim_leading_zeros_result%" == "$" set trim_leading_zeros_result=0
    exit /B 0

:get_sign
    set str=%~1
    set sign=%str:~0,1%
    set get_sign_result=
    if "$%sign%" == "$-" (
        set get_sign_result=Minus
        set get_sign_result_number=%str:~1%
    ) else if "$%sign%" == "$+" (
        set get_sign_result_number=%str:~1%
    ) else (
        set get_sign_result_number=%str%
    )
    exit /B 0

:remove_groups
    set str=%~1
    set remove_groups_result=%str:'=%
    exit /B 0

This is the test script I used:

@echo off
rem 10^x:x= 66  63  60  57  54  51  48  45  42  39  36  33  30  27  24  21  18  15  12   9   6   3   0
call number                                                                                          0
call number                                                                                          2
call number                                                                                        -17
call number                                                                                         30
call number                                                                                         48
call number                                                                                       -256
call number                                                                                        500
call number                                                                                        874
call number                                                                                      1'024
call number                                                                                    -17'001
call number                                                                                    999'999
call number                                                                                  1'048'576
call number                                                                         -1'000'001'000'000
call number                                                                    912'345'014'587'957'003
call number                                                       -999'912'345'014'587'124'337'999'999
call number                                        111'222'333'444'555'666'777'888'999'000'000'000'001
call number                               -912'345'014'587'912'345'014'587'124'912'345'014'587'124'337
call number    999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999
call number  1'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000
rem 10^x:x= 66  63  60  57  54  51  48  45  42  39  36  33  30  27  24  21  18  15  12   9   6   3   0

And this is the output I got from my test script:

0 -> Zero
2 -> Two
-17 -> Minus Seventeen
30 -> Thirty
48 -> Forty Eight
-256 -> Minus Two Hundred Fifty Six
500 -> Five Hundred
874 -> Eight Hundred Seventy Four
1'024 -> One Thousand Twenty Four
-17'001 -> Minus Seventeen Thousand One
999'999 -> Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine
1'048'576 -> One Million Forty Eight Thousand Five Hundred Seventy Six
-1'000'001'000'000 -> Minus One Trillion One Million
912'345'014'587'957'003 -> Nine Hundred Twelve Quadrillion Three Hundred Forty Five Trillion Fourteen Billion Five Hundred Eighty Seven Million Nine Hundred Fifty Seven Thousand Three
-999'912'345'014'587'124'337'999'999 -> Minus Nine Hundred Ninety Nine Septillion Nine Hundred Twelve Sextillion Three Hundred Forty Five Quintillion Fourteen Quadrillion Five Hundred Eighty Seven Trillion One Hundred Twenty Four Billion Three Hundred Thirty Seven Million Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine
111'222'333'444'555'666'777'888'999'000'000'000'001 -> One Hundred Eleven Undecillion Two Hundred Twenty Two Decillion Three Hundred Thirty Three Nonillion Four Hundred Forty Four Octillion Five Hundred Fifty Five Septillion Six Hundred Sixty Six Sextillion Seven Hundred Seventy Seven Quintillion Eight Hundred Eighty Eight Quadrillion Nine Hundred Ninety Nine Trillion One
-912'345'014'587'912'345'014'587'124'912'345'014'587'124'337 -> Minus Nine Hundred Twelve Tredecillion Three Hundred Forty Five Duodecillion Fourteen Undecillion Five Hundred Eighty Seven Decillion Nine Hundred Twelve Nonillion Three Hundred Forty Five Octillion Fourteen Septillion Five Hundred Eighty Seven Sextillion One Hundred Twenty Four Quintillion Nine Hundred Twelve Quadrillion Three Hundred Forty Five Trillion Fourteen Billion Five Hundred Eighty Seven Million One Hundred Twenty Four Thousand Three Hundred Thirty Seven
999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999'999 -> Nine Hundred Ninety Nine Vigintillion Nine Hundred Ninety Nine Novemdecillion Nine Hundred Ninety Nine Octodecillion Nine Hundred Ninety Nine Septendecillion Nine Hundred Ninety Nine Sexdecillion Nine Hundred Ninety Nine Quindecillion Nine Hundred Ninety Nine Quattuordecillion Nine Hundred Ninety Nine Tredecillion Nine Hundred Ninety Nine Duodecillion Nine Hundred Ninety Nine Undecillion Nine Hundred Ninety Nine Decillion Nine Hundred Ninety Nine Nonillion Nine Hundred Ninety Nine Octillion Nine Hundred Ninety Nine Septillion Nine Hundred Ninety Nine Sextillion Nine Hundred Ninety Nine Quintillion Nine Hundred Ninety Nine Quadrillion Nine Hundred Ninety Nine Trillion Nine Hundred Ninety Nine Billion Nine Hundred Ninety Nine Million Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine
1'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000'000 -> * ERR: the number is too big! Even wikipedia doesn't know how it's called!

If I could find some more names of large numbers, the script would support even bigger numbers. Currently, though, the script will work with all numbers from -(10^66-1) to (10^66-1).

I have to mention, that I had a lot of fun solving this in BATCH. :)