Linux shell - 除法保留小数点

转自:http://www.cnblogs.com/recognition/p/5459681.html

我想实现 举例:1/3=0.33得到0.33,

尝试过bc 只能得到.33,没有0,

linux 下的shell脚本,13是变量$a$b,并能指定小数点后的位数,

 

方法1

1
2
3
$> res=$(printf "%.5f" `echo "scale=5;1/3"|bc`)
$> echo $res
0.33333

 

方法2

1
2
3
$> ab=`echo "scale=5;a=1/3;if(length(a)==scale(a)) print 0;print a"|bc`
$> echo $ab
0.33333

 

posted @ 2017-09-25 15:58  sjxm2017  阅读(668)  评论(0编辑  收藏  举报