#!/bin/sh countd() { # count decimal places m="$1"; n=${2:-0}; while [ "$m" ]; do : $((n+=1)) next="${m%?}" [ "${m#$next}" = "." ] && { : $((n-=1)) break } || { m="$next" } done echo "$n" } ad(){ # add decimal to $1 based on $2 m="$1"; n=0; while [ "$m" ]; do next="${m%?}" [ "$n" -eq "$2" ] && break e="${m#$next}${e}" m="$next"; : $((n+=1)) done w="${1%$e}"; echo "${w:-0}.${e%0}" } [ -z "${1##*.*}" -o -z "${2##*.*}" ] || { # if not floats echo "$(($1*$2))" exit 0 } if [ ! -z "${1##*.*}" ]; then set -- "${1}.0" "$2"; d="${1#*.}"; d2="${2#*.}" elif [ ! -z "${2##*.*}" ]; then set -- "$1" "${2}.0"; d="${2#*.}"; d2="${1#*.}" fi # if $1/$2 is not a float; convert to float dn="$(countd $2 $(countd $1))"; m1="${1%.*}${1#*.}"; m2="${2%.*}${2#*.}" until [ "${m1#0}" = "$m1" ]; do m1="${m1#0}" done until [ "${m2#0}" = "$m2" ]; do m2="${m2#0}" done # dash has issues when numbers have leading 0's # zsh,bash,etc don't need this at all # and really dash shouldn't either but /shrug #printf '%s\n%s\n' "${m1}*${m2}" "9223372036854775807" ad "$((m1*m2))" "$dn"