linux 中实现浮点数的运算

 

001、bc实现

root@ubuntu01:/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'.
10 / 3                          ## 默认没有小数部分
3
scale=2                         ## 利用scale参数设定小数位数
10 / 3
3.33

 

 

简化:

root@ubuntu01:/home/test# temp=$(expr "10 / 3" | bc)
root@ubuntu01:/home/test# echo $temp
3
root@ubuntu01:/home/test# temp=$(expr "scale=3; 10 / 3" | bc)       ## 利用命令行直接计算
root@ubuntu01:/home/test# echo $temp
3.333

 

 

002、awk命令实现

root@ubuntu01:/home/test# awk 'BEGIN{printf 10 / 3"\n"}'
3.33333

 

posted @ 2022-09-15 09:29  小鲨鱼2018  阅读(293)  评论(0编辑  收藏  举报