首页 > 系统相关 >Linux学习4之shell脚本中的小数运算

Linux学习4之shell脚本中的小数运算

时间:2023-01-04 11:07:50浏览次数:48  
标签:BEGIN %. 2.45 Linux echo shell awk printf 小数


1.awk

hadoop@Mcnode1:~/cloud/adam/xubo/data/test20160310/test$ cat demical.sh 
#!/bin/bash
echo `awk -v x=2.45 -v y=3.123 'BEGIN{printf "%.2f\n",x*y}'`
s=1.5
echo $s
echo `awk -v x=2.45 -v y="$s" 'BEGIN{printf "%.2f\n",x*y}'`


结果:

hadoop@Mcnode1:~/cloud/adam/xubo/data/test20160310/test$ ./demical.sh 
7.65
1.5
3.68


可以从外部获取变量,也可以自定义变量


2.|bc

hadoop@Mcnode1:~/cloud/adam/xubo/data/test20160310/test$ cat demical.sh 
#!/bin/bash
echo `awk -v x=2.45 -v y=3.123 'BEGIN{printf "%.2f\n",x*y}'`
s=1.5
echo $s
echo `awk -v x=2.45 -v y="$s" 'BEGIN{printf "%.2f\n",x*y}'`
#product=`awk -v x=2.45 -v y="$s" 'BEGIN{printf "%.2f\n",x*y}'`

s2=$(echo "scale=3; 6 / 5" | bc)
echo $s2

s3=$(echo "scale=3; $s / 5" | bc)
echo $s3

结果:

hadoop@Mcnode1:~/cloud/adam/xubo/data/test20160310/test$ ./demical.sh 
7.65
1.5
3.68
1.200
.300




标签:BEGIN,%.,2.45,Linux,echo,shell,awk,printf,小数
From: https://blog.51cto.com/u_13791455/5987174

相关文章