你添加0.0005的技巧并不是一个坏主意.虽然,它并没有那么好用.当bc执行某些操作(如分区)时,内部使用scale.
在你的情况下,最好先执行除法,可能使用大规模或-l切换到bc1(如果你的版本支持它),然后加0.0005然后设置scale = 3并执行涉及内部扩展的操作执行截断.
就像是:
`a=$sum/$n+0.0005; scale=3; a/1`
当然,无论总和是正数还是负数,您都希望以不同方式进行.幸运的是,bc有一些条件运算符.
`a=$sum/$n; if(a>0) a+=0.0005 else if (a<0) a-=0.0005; scale=3; a/1`
然后,您将要使用printf格式化此答案.
包含在函数回合中(您可以选择小数位数):
round() {
# $1 is expression to round (should be a valid bc expression)
# $2 is number of decimal figures (optional). Defaults to three if none given
local df=${2:-3}
printf '%.*f\n' "$df" "$(bc -l <<< "a=$1; if(a>0) a+=5/10^($df+1) else if (a<0) a-=5/10^($df+1); scale=$df; a/1")"
}
试试吧:
gniourf$round "(3+3+4)/3"
3.333
gniourf$round "(3+3+5)/3"
3.667
gniourf$round "-(3+3+5)/3"
-3.667
gniourf$round 0
0.000
gniourf$round 1/3 10
0.3333333333
gniourf$round 0.0005
0.001
gniourf$round 0.00049
0.000
1使用-l开关,比例设置为20,这应该足够了.