# - loosely based on the function "round()", taken from
# - inspired by user85321 @ askubuntu.com (original author)
# and Aquarius Power
# the round function (alternate approach):
# if negative, negate value ...
[[ $(echo "$v < 0" | bc) -eq 1 ]] && v=$(echo "$v * -1" | bc)
r=$(echo "scale=$3;(((10^$3)*$v/$2)+0.5)/(10^$3)" | bc)
# ... however, since value was only negated to get correct rounding, we
# have to add the minus sign again for the resulting value ...
[[ $(echo "$vorig < 0" | bc) -eq 1 ]] && r=$(echo "$r * -1" | bc)
echo $(env printf %.$3f $r)
echo "Insert the price you want to calculate:"
echo "This is the price without taxes:"
echo $(round2 $float 1.18 2);
read -p "Press any key to continue..."
It is actually simple: there is no need to explicitly add a hardcoded "-0.5" variant for negative numbers. Mathematically spoken, we'll just compute the absolute value of the argument and still add 0.5 as we normally would. But since we (unfortunately) have no built-in
abs() function at our disposal (unless we code one), we will simply negate the argument if it's negative.
Besides, it proved very cumbersome to work with the quotient as a parameter (since for my solution, I must be able to access the dividend and divisor separately). This is why my script has an additional third parameter.