숫자를 완전한 서면 텍스트로 변환

숫자를 완전한 서면 텍스트로 변환

숫자를 완전한 텍스트로 변환하는 스크립트를 Linux에서 작성해야 합니다. 시간이 촉박해서 예외도 없고 쉼표 뒤의 숫자도 없는 코드를 풀어야했는데 그래도 안되네요. 프로그램이 변수를 인식하지 못하기 때문에 주로 구문과 관련된 도움이 필요합니다. 이것이 제가 지금 가지고 있는 것입니다(네덜란드어로).

업데이트: (제 생각에는) 가장 중요한 부분을 영어로 번역했습니다.

#!/bin/bash
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++ ))
do
    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";;
    esac
done
    for ((i=0 ; i<=$hundreds; i++ ))
do
    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";;
    esac
done
    for ((i=0 ; i<=$teens; i++ ))
do
    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";;
    esac
done
    for ((i=0 ; i<=$units; i++ ))
do
    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";;
    esac
done

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

답변1

코드에 몇 가지 문제가 있습니다. 첫 번째 문제는 백틱을 사용해야 하는 곳에 작은따옴표를 사용하고 있다는 것입니다. 두 번째 문제는 exprbash에서는 실제로 필요하지 않지만 $(())작업을 수행한다는 것입니다. 세 번째 문제는 공식이 단순히 잘못되었다는 것입니다. 네 번째 문제는 -prijs댓글에서 지적됩니다. 코드의 이 부분을 다시 작성하세요.

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

도착하다

thousands=$((price/1000))
hundreds=$((price%1000/100))
teens=$((price%100/10))
units=$((price%10))

%Bash에서 모듈로 연산자는 어디에 있습니까? -prijs나머지 부분(예: 스크립트의 내용 및 마지막 줄)을 직접 수정 해 보세요 .

답변2

흥미로운 도전입니다. 이것은 내 의견이다

#!/bin/bash

digits=(
    "" 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[@]}")

        ((u++))
        ((number = number / 1000))
    done
    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]}"
    else
        echo "${digits[num / 100]} hundred $("$FUNCNAME" $((num % 100)) )"
    fi
}

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]}"
    done
    echo "$1"
}

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

실행 중:

$ 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

답변3

미래의 나와 이 명령을 찾는 모든 사람을 위해.


탭 목록을 스크롤하는 동안 우연히 다음 명령을 발견했습니다.

spellout 1.0.11: convert numbers to number names and money amounts
Usage: spellout [-l lang] [-p prefix] par1 [par2...]
Parameter: n: number; n-m: range; n-m~s: range with step
Examples: spellout 1-10 500 1000-10000~1000
          spellout -l en-GB -p ordinal 1-100
          spellout -l en -p ordinal-number 1-100
          spellout -l en -p USD 100.45
          spellout -l en -p "money USD" 100.45
Help of language module: spellout -l es help
License: GNU LGPL/BSD dual-license

나는 그것이 (아마도) 다음에서 유래했다는 것을 알았습니다.번호텍스트/lib번호텍스트그리고 될 수있다데비안 소스 코드


예:

$ spellout 39825
thirty-nine thousand eight hundred twenty-five
$ spellout -l de-DE 39825
neununddreißigtausendachthundertfünfundzwanzig
$ spellout -l en-US -p "money GBP" 39825
thirty-nine thousand eight hundred twenty-five pounds sterling
$ spellout  -l de-DE -p ordinal 39825
neununddreißigtausendachthundertfünfundzwanzigste

관련 정보