linux中bc命令

在终端实现计算功能

1、在终端直接使用

root@PC1:/home/test# bc   ## 在终端直接输入bc进入
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
3 + 2  ## 实现加法
5
3 * 2  ##实现乘法
6
3 / 2  ##实现除法
1
3 - 2  ## 实现减法
1
3^2    ##实现指数
9
3%2    ## 求余数
1
quit     ##在终端输入quit退出bc

2、设定小数位数

root@PC1:/home/test# bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
scale=3  ## 设定小数位数为3
3 + 2
5
3 - 2
1
3 * 2
6
3 / 2     ## 3除以2的效果
1.500
3 ^ 2
9
3 % 2     ## 余数为什么是0??
0
3%2
0
scale=0   ## 小数位数设为0
3 % 2
1
scale=1
3 % 2
0
quit

3、另一种实现方式

root@PC1:/home/test# ls
root@PC1:/home/test# echo "3 + 2" | bc
5
root@PC1:/home/test# echo "3 * 3" | bc
9
root@PC1:/home/test# echo "3 ^ 2" | bc   ##平方
9
root@PC1:/home/test# echo "3 / 2" | bc    ## 商
1
root@PC1:/home/test# echo "3 - 2" | bc
1
root@PC1:/home/test# echo "3 % 2" |bc   ##余数
1
root@PC1:/home/test# ls
root@PC1:/home/test# echo "scale=2; 3 / 2" | bc   ## 指定小数位数
1.50
root@PC1:/home/test# echo "scale=2; 3.334 + 2.1143" | bc   ## 不能实现四舍五入??
5.4483
root@PC1:/home/test# echo "scale=2; 3 * 2" | bc
6
原文地址:https://www.cnblogs.com/liujiaxin2018/p/15803308.html