psh-fractional/multiplication/mul

42 lines
949 B
Bash
Executable file

#!/bin/sh
# $1 * $2
# count the total decimal places
countd() {
[ -z "${1##*.*}" ] && {
m="$1"; n=${2:-0}; while [ "$m" ]; do
: $((n+=1))
next="${m%?}"
[ "${m#$next}" = "." ] && {
break
} || {
m="$next"
}
done
} || n=1
echo "$n"
}
[ -z "${1##*.*}" -o -z "${2##*.*}" ] || { # if not floats
echo "$(($1*$2))"
exit 0
}
dn=$(countd "$2" $(countd "$1")); [ "$dn" -gt 2 ] && : $((dn-=2)) # decimal count
#echo "$dn | $(countd $1) -- $(countd $2)"
# remove all decimal places
if [ ! -z "${1##*.*}" ]; then # if $1 is not float
set -- "$1" "${2%.*}${2#*.}"
elif [ ! -z "${2##*.*}" ]; then
set -- "${1%.*}${1#*.}" "$2"
else
set -- "${1%.*}${1#*.}" "${2%.*}${2#*.}"
fi
#echo "$1 * $2"
o="$(($1*$2))"
# add back decimal point using $dn
n=0; m="$o"; [ "$dn" -gt 0 ] && while [ "$m" ]; do
next="${m%?}"
: $((n+=1))
[ "$n" -eq "$dn" ] && o="$next.${o#${m%?}}"
m="$next"
done
echo "$o"