Linux shell - 除法保留小數點
舉例:1/3=0.33得到0.33,
bc 只能得到.33,沒有0了,
linux 下的shell腳本,1和3是變量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