This commit is contained in:
olli 2024-06-24 15:58:37 +02:00
parent e1dbc5b9d1
commit 2563371b0e
2 changed files with 24 additions and 13 deletions

View File

@ -1,14 +1,18 @@
function g_num_valid_number { function g_num_valid_number {
[ -z "$1" ] && return 1 local arg
local f_arg=( "0" "$@" ) for arg in "$@"
local i
for (( i=0; i<=$#; i++ ))
do do
if ! [[ ${f_arg[$i]} =~ ^(-)?(\.)?[0-9]+(\.)?([0-9]+)?$ ]] if ! [[ $arg =~ ^(-)?(\.)?[0-9]+(\.)?([0-9]+)?$ ]]
then then
echo "\"${f_arg[$i]}\": Not a valid number" 1>&2 echo "\"$arg\": Not a valid number" 1>&2
g_traceback g_traceback
return 1 return 1
fi fi
done done
if [ -z "$1" ]
then
echo "No argument given" 1>&2
g_traceback
return 2
fi
} }

View File

@ -5,11 +5,18 @@ function g_percentage-diff {
local g_to=$2 local g_to=$2
unset g_percentage_diff_result unset g_percentage_diff_result
g_num_valid_number "$g_from" "$g_to" || return 1 g_num_valid_number "$g_from" "$g_to" || return 1
g_calc "${g_from} == 0" g_calc "$g_from == 0"
[ ${g_calc_result} -eq 1 ] && return 1 if [ $g_calc_result -eq 1 ]
g_calc "${g_to} == 0" then
[ ${g_calc_result} -eq 1 ] && return 1 g_echo_note "$0: ERR: g_from=$g_from"
g_calc "100/${g_from}*(${g_to}-${g_from})" return 2
#g_percentage_diff_result=$(printf -- "%.2f" "${g_calc_result}") fi
printf -v g_percentage_diff_result -- "%.2f" "${g_calc_result}" g_calc "$g_to == 0"
if [ $g_calc_result -eq 1 ]
then
g_echo_note "$0: ERR: g_to=$g_to"
return 3
fi
g_calc "100 / $g_from * ( $g_to - ( $g_from ) )" || return 4
printf -v g_percentage_diff_result -- "%.2f" $g_calc_result
} }