码迷,mamicode.com
首页 > 系统相关 > 详细

Linux shell - 除法保留小数点

时间:2016-05-04 21:13:48      阅读:1412      评论:0      收藏:0      [点我收藏+]

标签:

我想实现 举例: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

 

Linux shell - 除法保留小数点

标签:

原文地址:http://www.cnblogs.com/recognition/p/5459681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!