将数字转换为完整的书面文字

将数字转换为完整的书面文字

我必须在 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

我发现它(可能)来自Numbertext/libnumbertext并且可以在Debian 源代码


例子:

$ 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

相关内容