So this is a hackerrank problem ( https://www.hackerrank.com/challenges/bash-tutorials---arithmetic-operations ). Basically, input is an arithmetic expression, and I have to print out a formatted answer (3 decimal places). I tried it first
read exp echo "scale = 3; $exp" | bc -l
He passed several tests, but not the first.
5+50*3/20 + (19*2)/7 17.929 5+50*3/20 + (19*2)/7 answer is 17.929 , but my code outputs 17.928 . I tried this code instead
read exp printf "%.3f\n" `echo $exp | bc -l`
Note: the echo part should be in the opposite direction, but I put `` so as not to be confused with the quotation marks of the block. All tests passed. So what's the difference?
source share