如何在bash中使用bc四舍五入?

bla*_*edx 51 bash scripts bc

我想要使​​用 bash 脚本的快速示例:

#!/bin/bash
echo "Insert the price you want to calculate:"
read float
echo "This is the price without taxes:"
echo "scale=2; $float/1.18" |bc -l
read -p "Press any key to continue..."
bash scriptname.sh
Run Code Online (Sandbox Code Playgroud)

假设价格是:48.86 答案将是:41.406779661(实际上是 41.40 因为我正在使用scale=2;

我的问题是: 我如何舍入第二个小数以这种方式显示答案?:41.41

mig*_*gas 35

最简单的解决方案:

printf %.2f $(echo "$float/1.18" | bc -l)
Run Code Online (Sandbox Code Playgroud)

  • 也可以使用管道: `echo "$float/1.18" | BC -l | xargs printf %.2f` (4认同)
  • 正如上面的评论 (http://askubuntu.com/a/179949/512213) 中所指出的,不幸的是,对于负数,这会表现得不正确。 (2认同)
  • @RobertG:为什么?此解决方案不使用`+0.5`。试试 `printf "%.2f\n" "$(bc -l <​​<<"48.86/1.18")" "$(bc -l <​​<<"-48.86/1.18")"`,你会得到 `41.41 ` 和 `-41.41`。 (2认同)

小智 34

bash 轮函数:

round()
{
echo $(printf %.$2f $(echo "scale=$2;(((10^$2)*$1)+0.5)/(10^$2)" | bc))
};
Run Code Online (Sandbox Code Playgroud)

在您的代码示例中使用:

#!/bin/bash
# the function "round()" was taken from 
# http://stempell.com/2009/08/rechnen-in-bash/

# the round function:
round()
{
echo $(printf %.$2f $(echo "scale=$2;(((10^$2)*$1)+0.5)/(10^$2)" | bc))
};

echo "Insert the price you want to calculate:"
read float
echo "This is the price without taxes:"
#echo "scale=2; $float/1.18" |bc -l
echo $(round $float/1.18 2);
read -p "Press any key to continue..."
Run Code Online (Sandbox Code Playgroud)

祝你好运:o)


zub*_*ber 22

Bash/awk 舍入:

echo "23.49" | awk '{printf("%d\n",$1 + 0.5)}'  
Run Code Online (Sandbox Code Playgroud)

如果你有 python,你可以使用这样的东西:

echo "4.678923" | python -c "print round(float(raw_input()))"
Run Code Online (Sandbox Code Playgroud)

  • 对于 Python,您可以使用类似 `python -c "p​​rint(round($num))"` 之类的东西,其中 `num=4.678923`。没有必要与标准输入混为一谈。你也可以像这样舍入到 n 位数字:`python -c "p​​rint(round($num, $n))"`。 (2认同)

小智 7

这是一个纯粹的 bc 解决方案。舍入规则:在 +/- 0.5 处,从零开始舍入。

将您要查找的比例放在 $result_scale 中;你的数学应该是 $MATH 在 bc 命令列表中的位置:

bc <<MATH
h=0
scale=0

/* the magnitude of the result scale */
t=(10 ^ $result_scale)

/* work with an extra digit */
scale=$result_scale + 1

/* your math into var: m */
m=($MATH)

/* rounding and output */
if (m < 0) h=-0.5
if (m > 0) h=0.5

a=(m * t + h)

scale=$result_scale
a / t
MATH
Run Code Online (Sandbox Code Playgroud)

  • 很有意思!`MATH=-0.34;result_scale=1;bc &lt;&lt;MATH`, #ObjectiveAndClear:5 解释 [here](https://gist.github.com/AquariusPower/ee2b7fc44e674296a324) :) (2认同)

小智 5

这是脚本的缩写版本,已修复以提供您想要的输出:

#!/bin/bash
float=48.86
echo "You asked for $float; This is the price without taxes:"
echo "scale=3; price=$float/1.18 +.005; scale=2; price/1 " | bc
Run Code Online (Sandbox Code Playgroud)

请注意,向上舍入到最接近的整数相当于加上 0.5 并取整,或向下舍入(对于正数)。

此外,比例因子在操作时应用;所以(这些是bc命令,您可以将它们粘贴到终端中):

float=48.86; rate=1.18; 
scale=2; p2=float/rate
scale=3; p3=float/rate
scale=4; p4=float/rate
print "Compare:  ",p2, " v ", p3, " v ", p4
Compare:  41.40 v 41.406 v 41.4067

# however, scale does not affect an entered value (nor addition)
scale=0
a=.005
9/10
0
9/10+a
.005

# let's try rounding
scale=2
p2+a
41.405
p3+a
41.411
(p2+a)/1
41.40
(p3+a)/1
41.41
Run Code Online (Sandbox Code Playgroud)