bash shell 脚本中的浮点数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12702185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:26:46  来源:igfitidea点击:

float numbers in bash shell script

bashfloating-point

提问by arsenal

Possible Duplicate:
Division in script and floating-point

可能的重复:
脚本和浮点除法

Mismatch  Percentage: $((( 100 * $TEST2 ) / $TEST1))

I am trying to get float number from above code. I am using bash shell script. But the above code is not giving me float numbers. I guess bash does not support floating numbers

我试图从上面的代码中获取浮点数。我正在使用 bash shell 脚本。但是上面的代码没有给我浮点数。我猜 bash 不支持浮点数

回答by Rody Oldenhuis

Shameless steal from VaughnCato:

从 VaughnCato 无耻的偷窃:

echo "Mismatch Percentage: $(echo "100*$TEST2/$TEST1" | bc -l)" 

回答by Bernhard

I have defined a function in ~./bashrc

我已经定义了一个函数 ~./bashrc

function bashCalc ()
{
    bc <<< "scale=6;";
}

Now you can always use in your (sub)shell:

现在你总是可以在你的(子)shell 中使用:

Mismatch Percentage: $(bashCalc (100*$TEST2)/$TEST1 )

See also: https://unix.stackexchange.com/questions/40786/how-can-i-do-command-line-integer-float-calculations-in-bash-or-any-language

另见:https: //unix.stackexchange.com/questions/40786/how-can-i-do-command-line-integer-float-calculations-in-bash-or-any-language