Linux shell - 除法保留小数点

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

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

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

 

方法1

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

 

方法2

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

 

posted @ 2016-05-04 20:32  灵风_滴水穿石  阅读(23473)  评论(0编辑  收藏  举报