浮点数的unix shell脚本减法 [英] unix shell scripting subtraction for floating point numbers

查看:58
本文介绍了浮点数的unix shell脚本减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#!/bin/ksh

a=8.3
b=10.20
diff=`expr $b - $a`
echo "$diff"

它的付出

expr: 0402-046 指定的运算符需要数字参数.错误我希望输出为 1.9

expr: 0402-046 A specified operator requires numeric parameters. error i want output as 1.9

推荐答案

为此你不需要 bc,使用 ksh 中的原生算术运算符

You don't need bc for this, use the native arithmetic operators in ksh

#!/bin/ksh

a=8.3
b=10.20
printf "%.2f\n" "$((b - a))"

输出

$ ksh script.ksh
1.90

这篇关于浮点数的unix shell脚本减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆