linux 小数点如何相加

linux 小数点如何相加,第1张

shell不可直接进行小数运算,可以用去其他方法,这是我想到的

1:用bc方法,你可以去查查

echo $us+$sy | bc

如我的结果

# echo $a

8.8

# echo $b

9.1

# echo $a+$b | bc

17.9

2:用awk方法

# awk -v a=$a -v b=$b 'BEGIN{print a+b}'

17.9

原来我�‹.shell写的计算器:[root@liuxiting testdir]# cat calculator.sh #!/bin/bashecho "usage: 1+3 <Enter>,q <Enter>is quit"while [ 1 ]doread -p "->>" str 1>>/dev/nulla=`echo $str |awk -F '+|-|*|/' '{print $1}'`if [ $a == q ]thenbreakfib=`echo $str |awk -F '+|-|*|/' '{print $2}'`o=`echo $str |grep -o "[[:punct:]]" | grep -v "\."`case $o in+) awk 'BEGIN{printf " =%.2f\n",'$a'+'$b'}'-) awk 'BEGIN{printf " =%.2f\n",'$a'-'$b'}'\*) awk 'BEGIN{printf " =%.2f\n",'$a'*'$b'}'/)if [ $b -eq 0 ]then echo 0 Can NOT be denominator!continuefi awk 'BEGIN{printf " =%.2f\n",'$a'/'$b'}'#^) awk 'BEGIN{printf " =%.2f\n",'$a'**'$b'}'*) echo erroresacdone 刚刚写的倒序输出:[root@liuxiting testdir]# cat daoXuShuChu.sh #!/bin/bash echo "usage: 123456 <Enter>, q <Enter>is quit"while [ 1 ]doecho -n "Pleasw enter number : " read n if [ $n == 'q' ]then breakfisd=0 rev="" on=$n echo "$n" while [ $n -gt 0 ] do sd=$(( $n % 10 )) # get Remainder n=$(( $n / 10 )) # get next digit rev=$( echo $rev$sd) done echo "$on in a reverse order $rev" done


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7358443.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-04
下一篇 2023-04-04

发表评论

登录后才能评论

评论列表(0条)

保存