Four is magic: Difference between revisions
→{{header|UNIX Shell}}: Fix bug in name translation.
m (→{{header|REXX}}: added whitespace.) |
(→{{header|UNIX Shell}}: Fix bug in name translation.) |
||
Line 3,272:
=={{header|UNIX Shell}}==
{{works with|Bash|4+}}
<lang sh>#
declare -A names=([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]=eighteen [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 [1000000000000]=trillion▼
[1000000000000000]=quadrillion [1000000000000000000]=quintillion)▼
# The powers of 10 above 10, in descending order▼
powers_of_10=($(printf '%s\n' "${!names[@]}" | sort -nr | grep '00$'))▼
# Function to return the name of a number given in numeric form▼
name_of() {
local -i n=$1 ▼
# Names for numbers that fit in a bash integer
▲ [
[1000000000000]=trillion [1000000000000000]=quadrillion
▲ # The powers of 10 above 10, in descending order
▲ local powers_of_10=($(printf '%s\n' "${!names[@]}" | sort -nr | grep '00$'))
printf '%s%s\n' "${names[$n]}" ${suffix:+" $suffix"}▼
# find the largest power of 10 that is smaller than n
local -i i=0
Line 3,309 ⟶ 3,299:
# if we found one, split on it and construct quotient 'name' remainder
if (( n >= p )); then
local -i quotient=n/p
Line 3,318 ⟶ 3,306:
remname=$(name_of $remainder)
fi
elif (( n > 20 )); then
else▼
# things are a little different under 100, since the multiples of
# 10 have their own names
Line 3,328 ⟶ 3,316:
remname=-$(name_of $remainder)
fi
printf '
▲ else
fi
|