How would I round the result from two divided numbers, e.g.
3/2
As when I do
testOne=$((3/2))
$testOne contains "1" when it should have rounded up to "2" as the answer from 3/2=1.5
How would I round the result from two divided numbers, e.g.
3/2
As when I do
testOne=$((3/2))
$testOne contains "1" when it should have rounded up to "2" as the answer from 3/2=1.5
To do rounding up in truncating arithmetic, simply add (denom-1) to the numerator.
Example, rounding down:
N/2 M/5 K/16
Example, rounding up:
(N+1)/2 (M+4)/5 (K+15)/16
To do round-to-nearest, add (denom/2) to the numerator (halves will round up):
(N+1)/2 (M+2)/5 (K+8)/16
bash will not give you correct result of 3/2 since it doesn't do floating pt maths. you can use tools like awk
$ awk 'BEGIN { rounded = sprintf("%.0f", 3/2); print rounded }'
2
or bc
$ printf "%.0f" $(echo "scale=2;3/2" | bc)
2