This is a hackerrank problem:
As a brief summary, you need to sum up a predefined number of integers, then compute the average and print it with 3 digits precision.
I came up with the following code:
read number_of_ints;
sum=0
for number in $(seq 1 $number_of_ints); do
read number
sum=$(($sum+$number))
done
printf "%.3f\n" | bc -l <<< $sum/$number_of_ints
printf "%.3f\n" $(echo "$sum/$number_of_ints" | bc -l)
However, printf "%.3f\n" | bc -l <<< $sum/$number_of_ints
blatantly ignores my format string and simply outputs it with 20 digit precision.
Meanwhile printf "%.3f\n" $(echo "$sum/$number_of_ints" | bc -l)
does exactly what I want.
I get that the 20 precision digits are rooted in the fact that bc -l preloads a math library, but shouldn't printf "%.3f" still cut this down to three digits?