bc小技巧

bc操作

bc的特点是支持小数点运算

1)计算5.4+5
[root@linhexiao shellTest]# echo 5.4+5 |bc
10.4
2)计算5.4+5时,不合发的数字不能计算
[root@linhexiao shellTest]# echo 5. 4+ 5 |bc
(standard_in) 1: syntax error
3)只要是合法计算表达式就可以用bc计算
[root@linhexiao shellTest]# echo 5.4+ 5 |bc
10.4
[root@linhexiao shellTest]# echo 5.4 + 5 |bc
10.4
4)计算1+2+3+4+5+6+7+8+9+10
[root@linhexiao shellTest]# seq -s "+" 10
1+2+3+4+5+6+7+8+9+10
[root@linhexiao shellTest]# seq -s "+" 10 | bc
55
5)计算1*2*3*4*5*6*7*8*9*10
[root@linhexiao shellTest]# seq -s "*" 10 | bc
3628800
6)结果保留2位小数点
[root@linhexiao shellTest]# echo "scale=2;5.13/4.23" | bc
1.21
7)结果保留20位小数点
[root@linhexiao shellTest]# echo "scale=20;5.13/4.23" | bc
1.21276595744680851063
8)将8转化为2进制
[root@linhexiao shellTest]# echo "obase=2;8" | bc
1000
9)将14转化为16进制
[root@linhexiao shellTest]# echo "obase=16;14" | bc
E

猜你喜欢

转载自linhexiao.iteye.com/blog/2288440