Script calculator division

自闭症网瘾萝莉.ら 提交于 2019-12-14 02:34:51

问题


i have a problem with my syntax below, i want to output the division of two numbers in decimal,but if i input 4 and 5, my output is 0 not 0.8 when i divide all others work fine.

case $ch in
  1)sum=`expr $n1 + $n2`
   echo "Sum ="$sum;;
  2)sum=`expr $n1 - $n2`
    echo "Sub = "$sum;;
  3)sum=`expr $n1 \* $n2`
    echo "Mul = "$sum;;
  4)sum=`expr $n1 / $n2`
    echo "Div = "$sum;;
  *)echo "Invalid choice";;
esac

回答1:


Expr only implements integer arithmetic. Look into using something else, like dc.




回答2:


Use bc.

sum=`echo -e "scale=5\n$n1/$n2" | bc`

Your division will give 0.8 with this.



来源:https://stackoverflow.com/questions/10429943/script-calculator-division

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!