I have to write a script in llinux that converts numbers into full written words. Due to a shortage in time ill have to settle for a code without the exeptions and numbers after the comma, but it still doesn't work. The program doesn't recognize the variables so I mostly need assistance involving the syntax. This is what I have for now. (in dutch) Update: I translated the most important (I think) part in english.

echo -prijs "Give the price: "
read price

thousands='expr $price /1000'
hundreds='expr ($price - $thousands) / 100'
teens='expr ($price - $hundreds - $thousands / 10'
units='expr $price - $hundreds - $thousands - $teens'

    for ((i=0 ; i<=$thousands; i++ ))
    case $thousands in
        0) echo -prijs "";;
        1) echo -prijs "duizend";;
        2) echo -prijs "tweeduizend";;
        3) echo -prijs "drieduizend";;
        4) echo -prijs "vierduizend";;
        5) echo -prijs "vijfduizend;;
        6) echo -prijs "zesduizend";;
        7) echo -prijs "zevenduizend";;
        8) echo -prijs "achtduizend";;
        9) echo -prijs "negenduizend";;
        10) echo -prijs "tienduizend";;
    for ((i=0 ; i<=$hundreds; i++ ))
    case $hundreds in
        0) echo -prijs "";;
        1) echo -prijs "honderd";;
        2) echo -prijs "tweehonderd";;
        3) echo -prijs "driehonderd";;
        4) echo -prijs "vierhonderd";;
        5) echo -prijs "vijfhonderd";;
        6) echo -prijs "zeshonderd";;
        7) echo -prijs "zevenhonderd";;
        8) echo -prijs "achthonderd";;
        9) echo -prijs "negenhonderd";;
    for ((i=0 ; i<=$teens; i++ ))
    case $teens in
        0) echo -prijs "";;
        1) echo -prijs "tien";;
        2) echo -prijs "twintig";;
        3) echo -prijs "dertig";;
        4) echo -prijs "veertig";;
        5) echo -prijs "vijftig";;
        6) echo -prijs "zestig";;
        7) echo -prijs "zeventig";;
        8) echo -prijs "tachtig";;
        9) echo -prijs "negentig";;
    for ((i=0 ; i<=$units; i++ ))
    case $units in
        0) echo -prijs "";;
        1) echo -prijs "een";;
        2) echo -prijs "twee";;
        3) echo -prijs "drie";;
        4) echo -prijs "vier";;
        5) echo -prijs "vijf";;
        6) echo -prijs "zes";;
        7) echo -prijs "zeven";;
        8) echo -prijs "acht";;
        9) echo -prijs "negen";;

echo "The price is: " 'expr $thousands + $hundreds + $teens + $units'

There are several issues with your code. The first issue is that you're using single quotes where you should use backticks. The second issue is that expr is not really needed in bash, $(()) will do the job. The third issue is that the formula is simply wrong. The fourth issue is the -prijs as pointed out in the comment. Rewrite this part of your code,

thousands='expr $price /1000'
hundreds='expr ($price - $thousands) / 100'
teens='expr ($price - $hundreds - $thousands / 10'
units='expr $price - $hundreds - $thousands - $teens'



where % is the modulo operator in bash. Try to fix the rest (for example the -prijs stuff, and the last line of your script) by yourself.


An interesting challenge. Here's my take


    "" one two three four five six seven eight nine
    ten eleven twelve thirteen fourteen fifteen sixteen seventeen eightteen nineteen
tens=("" "" twenty thirty forty fifty sixty seventy eighty ninety)
units=("" thousand million billion trillion)

number2words() {
    local -i number=$((10#$1))
    local -i u=0
    local words=()
    local group

    while ((number > 0)); do
        group=$(hundreds2words $((number % 1000)) )
        [[ -n "$group" ]] && group="$group ${units[u]}"

        words=("$group" "${words[@]}")

        ((number = number / 1000))
    echo "${words[*]}"

hundreds2words() {
    local -i num=$((10#$1))
    if ((num < 20)); then
        echo "${digits[num]}"
    elif ((num < 100)); then
        echo "${tens[num / 10]} ${digits[num % 10]}"
        echo "${digits[num / 100]} hundred $("$FUNCNAME" $((num % 100)) )"

with_commas() {
    # sed -r ':a;s/^([0-9]+)([0-9]{3})/\1,\2/;ta' <<<"$1"
    # or, with just bash
    while [[ $1 =~ ^([0-9]+)([0-9]{3})(.*) ]]; do
        set -- "${BASH_REMATCH[1]},${BASH_REMATCH[2]}${BASH_REMATCH[3]}"
    echo "$1"

for arg; do
    [[ $arg == *[^0-9]* ]] && result="NaN" || result=$(number2words "$arg")
    printf "%s\t%s\n" "$(with_commas "$arg")" "$result"

In action:

$ bash ./num2text.sh 9 98 987 9786 98765 987654 9876543 98765432 987654321 9876543210 98765432100 987654321000 9876543210000 98765432100000 987654321000000 1,234 x 1y z2
9       nine
98      ninety eight
987     nine hundred eighty seven
9,786   nine thousand seven hundred eighty six
98,765  ninety eight thousand seven hundred sixty five
987,654 nine hundred eighty seven thousand six hundred fifty four
9,876,543       nine million eight hundred seventy six thousand five hundred forty three
98,765,432      ninety eight million seven hundred sixty five thousand four hundred thirty two
987,654,321     nine hundred eighty seven million six hundred fifty four thousand three hundred twenty one
9,876,543,210   nine billion eight hundred seventy six million five hundred forty three thousand two hundred ten
98,765,432,100  ninety eight billion seven hundred sixty five million four hundred thirty two thousand one hundred
987,654,321,000 nine hundred eighty seven billion six hundred fifty four million three hundred twenty one thousand
9,876,543,210,000       nine trillion eight hundred seventy six billion five hundred forty three million two hundred ten thousand
98,765,432,100,000      ninety eight trillion seven hundred sixty five billion four hundred thirty two million one hundred  thousand
987,654,321,000,000     nine hundred eighty seven trillion six hundred fifty four billion three hundred twenty one million
1,234   NaN
x       NaN
1y      NaN
z2      NaN

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.