计算表达式并四舍五入到三位小数

Wan*_*der 4 bash shell-script arithmetic bc floating-point

我有一个表达式,"5+50*3/20 + (19*2)/7"我需要将它四舍五入到小数点后 3 位。答案是17.92857142857143。当我使用下面的脚本时,它给了我17.928. 答案应该是17.929

read exp
echo "scale=3; $exp" |bc -l
Run Code Online (Sandbox Code Playgroud)

还有一个问题是如何使用printf来完成相同的任务

Ami*_*4x7 8

只写这个:

read exp
printf %.3f $(echo "$exp" | bc -l)
Run Code Online (Sandbox Code Playgroud)