2022-01-24 19:58:36 +00:00
|
|
|
#!/bin/sh
|
|
|
|
s() {
|
|
|
|
n=$1; i=1; while [ "$n" -le $2 ]; do
|
|
|
|
echo "$n"; : $((n+=i))
|
|
|
|
done
|
|
|
|
} # seq
|
|
|
|
fd() { # return first digit of $1
|
|
|
|
x="$1"; until [ "${#x}" -eq 1 ]; do
|
|
|
|
x="${x%?}"
|
|
|
|
done
|
|
|
|
echo "$x"
|
|
|
|
}
|
2022-02-23 16:26:42 +00:00
|
|
|
countd() { # count decimal places
|
|
|
|
m="$1"; n=${2:-0}; while [ "$m" ]; do
|
|
|
|
: $((n+=1))
|
|
|
|
next="${m%?}"
|
|
|
|
[ "${m#$next}" = "." ] && {
|
|
|
|
break
|
|
|
|
} || {
|
|
|
|
m="$next"
|
|
|
|
}
|
|
|
|
done
|
|
|
|
echo "$n"
|
|
|
|
}
|
2022-01-24 19:58:36 +00:00
|
|
|
ld() { # $1/$2
|
2022-02-24 16:57:55 +00:00
|
|
|
|
2022-02-23 16:26:42 +00:00
|
|
|
if [ ! -z "${1##*.*}" -a -z "${2##*.*}" ]; then
|
|
|
|
di="${1}."; until [ "${#di}" -eq "$((${#2}+1))" ]; do
|
|
|
|
di="${di}0"
|
|
|
|
done
|
|
|
|
set -- "$di" "$2" "$3"
|
|
|
|
elif [ ! -z "${2##*.*}" -a -z "${1##*.*}" ]; then
|
|
|
|
div="${2}."; until [ "${#div}" -eq "$((${#1}+1))" ]; do
|
|
|
|
div="${div}0"
|
|
|
|
done
|
|
|
|
set -- "$1" "$div" "$3"
|
|
|
|
fi
|
2022-02-24 16:57:55 +00:00
|
|
|
|
2022-02-23 16:26:42 +00:00
|
|
|
[ -z "${1##*.*}" -o -z "${2##*.*}" ] || {
|
|
|
|
set -- "$1.00" "$2.00" "$3"
|
|
|
|
}
|
2022-02-24 16:57:55 +00:00
|
|
|
|
2022-02-23 16:26:42 +00:00
|
|
|
di="${1##*.}"; div="${2##*.}"
|
2022-02-24 16:57:55 +00:00
|
|
|
until [ "${#di}" -ge "${#div}" ]; do
|
2022-02-23 16:26:42 +00:00
|
|
|
di="${di}0"
|
|
|
|
done
|
2022-02-24 16:57:55 +00:00
|
|
|
until [ "${#div}" -ge "${#di}" ]; do
|
2022-02-23 16:26:42 +00:00
|
|
|
div="${div}0"
|
|
|
|
done
|
|
|
|
set -- "${1%%.*}.$di" "${2%%.*}.$div" "$3"
|
|
|
|
# handle decimal less numbers
|
2022-02-24 16:57:55 +00:00
|
|
|
|
2022-02-23 16:26:42 +00:00
|
|
|
#echo "$1/$2"
|
2022-02-24 16:57:55 +00:00
|
|
|
|
2022-02-14 15:32:47 +00:00
|
|
|
di="$1"; div="$2"; count="$3"
|
2022-01-24 19:58:36 +00:00
|
|
|
case "$div" in
|
2022-02-24 16:57:55 +00:00
|
|
|
*'.'*) # if $2 has a decimal we must shift
|
|
|
|
set -- "${1%.*}${1#*.}" "${2%.*}${2#*.}" "$3" # shift $2 # simply remove the decimal point
|
|
|
|
until [ "${#1}" -ge "${#2}" ]; do
|
|
|
|
set -- "${1}0" "$2" "$3"
|
2022-01-24 19:58:36 +00:00
|
|
|
done
|
2022-02-24 16:57:55 +00:00
|
|
|
set -- "$1.00" "$2" "$3" # add decimals back in for $1 only
|
2022-01-24 19:58:36 +00:00
|
|
|
esac
|
2022-02-24 16:57:55 +00:00
|
|
|
#echo "$1 $2"
|
|
|
|
di="$1"; div="$2"; count="$3" # redeclare
|
2022-02-23 16:26:42 +00:00
|
|
|
#echo "$sp $div ${di}/${#di} | $1/$2 | $di/$div"
|
2022-02-24 16:57:55 +00:00
|
|
|
o="$((${di%.*}/${div%.*}))"; [ "${#o}" -eq 0 ] && : $((count+=2)); : $((count+=${#o}))
|
|
|
|
sd="${di%.*}"; : $((count+=${#sd}));
|
|
|
|
until [ "${#di}" -eq "$((count+1))" ]; do
|
2022-01-24 19:58:36 +00:00
|
|
|
di="${di}0"
|
|
|
|
done
|
2022-02-24 16:57:55 +00:00
|
|
|
: $((count-=1)) && di="${di%0}"
|
|
|
|
set -- "$1" "$2" "${3:-9}" "$di"
|
2022-02-22 17:33:22 +00:00
|
|
|
unset IFS o; n=0; for i in $(s 1 $count); do
|
2022-01-24 19:58:36 +00:00
|
|
|
fd="$(fd $di)" # fd is needed later
|
2022-02-24 16:57:55 +00:00
|
|
|
[ "$fd" = '.' ] && {
|
|
|
|
dp="$n"
|
|
|
|
di="${di#?}"; fd="$(fd $di)"
|
|
|
|
}
|
2022-01-24 19:58:36 +00:00
|
|
|
[ "$i" -gt 1 ] && {
|
|
|
|
fd="${sd}${fd}"
|
|
|
|
#echo "$fd"
|
|
|
|
}
|
2022-02-22 17:33:22 +00:00
|
|
|
sd="$((fd/div))"; [ "$sd" -eq 0 ] && : $((count+=1))
|
|
|
|
o="${o}${sd}"; sd="$((sd*div))"
|
|
|
|
#printf " %s\n- %s\n" "$fd" "$sd"
|
|
|
|
sd="$((fd-sd))"
|
2022-02-24 16:57:55 +00:00
|
|
|
#echo "| $sd -- $i"
|
|
|
|
di="${di#?}" # $fd$sd
|
|
|
|
: $((n+=1))
|
|
|
|
done
|
|
|
|
n=0; sd="$o"; while [ "$sd" ]; do
|
|
|
|
next="${sd#?}"
|
|
|
|
[ "$n" -eq "$dp" ] && {
|
|
|
|
o="${o%$sd}.$sd"
|
2022-02-22 17:33:22 +00:00
|
|
|
break
|
|
|
|
}
|
2022-02-24 16:57:55 +00:00
|
|
|
sd="$next"
|
2022-02-22 17:33:22 +00:00
|
|
|
: $((n+=1))
|
2022-01-24 19:58:36 +00:00
|
|
|
done
|
2022-03-09 15:28:30 +00:00
|
|
|
#until [ "${o#0}" = "$o" ]; do
|
|
|
|
# o="${o#0}"
|
|
|
|
#done
|
2022-02-24 16:57:55 +00:00
|
|
|
#echo "$o"
|
2022-02-23 16:26:42 +00:00
|
|
|
if [ ! -z "${o##*.*}" -a "$dp" -gt 0 ]; then # missing decimal place
|
|
|
|
do="$o"; [ "${#do}" -gt "$dp" ] && until [ "${#do}" -eq "$dp" ]; do
|
|
|
|
do="${do%?}"
|
|
|
|
done
|
|
|
|
o="${do:-0}.${o#$do}"
|
|
|
|
fi
|
2022-02-24 16:57:55 +00:00
|
|
|
[ "$(fd $o)" = '.' ] && o="0$o"
|
2022-03-09 15:28:30 +00:00
|
|
|
until [ "${o#0}" = "$o" ]; do
|
|
|
|
o="${o#0}"
|
|
|
|
done
|
|
|
|
[ "$(fd $o)" = '.' ] && o="0${o}"
|
2022-02-24 16:57:55 +00:00
|
|
|
echo "$o" # | $1/$2 | $dp | $do || $fd $1"
|
2022-01-24 19:58:36 +00:00
|
|
|
}
|
2022-03-09 15:28:30 +00:00
|
|
|
# 19 decimal points unless $3 is passed # bc uses 19
|
2022-04-05 16:24:44 +00:00
|
|
|
[ "$1" -a "$2" ] && ld "$1" "$2" "${3:-20}"
|