本文主要是介绍linux做算术运算- bc命令和 $(()),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
如何保证小数点
# bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
scale=10
17234/84319
.2043904695
就是说,通过scale环境变量来控制小数点的位数。如果不指定改环境变量,即便数是浮点数,仍然会当做整数来计算
[@djt_137_216 p2]# bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
17234/84319
0
17234/84319.0
0
转载:http://blog.csdn.net/yangtalent1206/article/details/12997469
通过管道使用bc来计算
[root@localhost centos39]# echo 3 * 4 | bc
(standard_in) 1: parse error
[root@localhost centos39]# echo "3 * 4" | bc
12
[root@localhost centos39]# echo "scale=7; 355/113" | bc
3.1415929
[root@localhost centos39]#
示例三 进制转换
[root@rhel55 ~]# echo "ibase=16; FFFF" | bc
65535
[root@rhel55 ~]# echo "obase=16; 1000" | bc
3E8
[root@rhel55 ~]#
我们用到bc的ibase和obase方法。
ibase是输入数字的进制,而obase就是输出数字的进制了。很好记,i是input,o是output。
如果用一条命令来转数字,可以用echo命令和管道结合bc。如下:
10进制转2进制:
$ echo "obase=2;ibase=10;100" | bc1100100
10进制转16进制:
$ echo "obase=16;ibase=10;100" | bc64
16进制转10进制:
$ echo "ibase=16;obase=2;F1" | bc11110001
注意,16进制数字的F要大写,如果小写结果是不对的。像最上面没有指定的时候,默认是10进制。
示例四 将多个表达式写在一个文件中一起计算
[root@rhel55 ~]# cat test.bc
123*321
123/321
scale=4;123/321
area=$(echo "scale=2;(1/2) * $b * $h"|bc)
这样就可以在shell做算术运算了
[@djt_137_216 p2]# echo "3*4"
3*4
[@djt_137_216 p2]# echo "3*4" | bc
12
[@djt_137_216 p2]# echo $((3*4))
12
calc_fee()
> {
> bc -q <<EOF
> 0.05*$1
> EOF
> }
[root@web ~]# calc_fee 1000
50.00
$(()) 中的变量是否一定要$
在使用$(())时,如果里面引用一个变量时,在shell中,该变量名可以带上$符号,但也可以不带。
# var=4
# echo $(($var + 1))
5
# echo $((var + 1))
5
# echo $((var ** 3))
64
# echo $(($var ** 3))
64
就是说,$(())做数学运算时,里面的变量可以用$,也可以不用!!
$(())在数字转换中的应用
# for dt in 2014-04-0{1..9} 2014-04-{10..30}; do day=${dt:8:10}; WIND=$(($day + 6)); echo "$day $WIND"; done
01 7
02 8
03 9
04 10
05 11
06 12
07 13
-bash: 08: value too great for base (error token is "08")
Numbers starting with leading 0 are Octal numbers (base 8) in many programming
languages including C, Perl and shell. Valid octal digits are
0,1,2,3,4,5,6,7 so it barfs if it sees an 8 or a 9. You probably want
to work in straight numbers and make a leading 0 in your output
format with a sprintf("%02d") kind of formatting thing.
Anything starting with 0x or 0X is a hex number.So the error message means exactly as it says- it's an error from
the let function complaining about the value being too big for the base.
Answer:
You can explicitly state the base of a number using base#number
Code:
if [ $((10#$item)) -eq 0 ] ; then
That will have trouble if the number starts with a minus sign.
The '-' needs to be in front of the base like -10#009 for -9.
并且:
# var=08
# echo $((10#var))
-bash: 10#var: value too great for base (error token is "10#var")
# echo $((10#$var))
8
在上面的例子$(())中引用的变量一定要用$符号标志,这与我们上面提到的有点儿不一样,但这恰恰是shell的灵活之处,但为了书写一致, $(())中引用变量时一定要加上$符号。
各个进制数字转换为10进制的做法
# var=012
# echo $((8#$var))
10
# echo $((10#$var))
12
# var=12
# echo $((2#$var))
-bash: 2#12: value too great for base (error token is "2#12")
# echo $((8#$var))
10
# echo $((16#$var))
18
# echo $((16#$var))
可以直接读入数字的,可能是你乘的时候出问题了,*转义了吗?
我刚写了一个脚本,能显示正确的结果:
[code=BatchFile]
#!/bin/bash
echo "input a number:"
read n
echo `expr $n \* 3`
[/code]
输入4,显示结果12,是对的。
10进制转换为其他进制的数字
使用上面的bc命令
还有http://blog.csdn.net/qianlong4526888/article/details/8516461
这篇关于linux做算术运算- bc命令和 $(())的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!