How to compare two floating point numbers in Bash?

前端 未结 17 2140
無奈伤痛
無奈伤痛 2020-11-22 02:17

I am trying hard to compare two floating point numbers within a bash script. I have to variables, e.g.

let num1=3.17648e-22
let num2=1.5

No

17条回答
  •  粉色の甜心
    2020-11-22 02:48

    Use korn shell, in bash you may have to compare the decimal part separately

    #!/bin/ksh
    X=0.2
    Y=0.2
    echo $X
    echo $Y
    
    if [[ $X -lt $Y ]]
    then
         echo "X is less than Y"
    elif [[ $X -gt $Y ]]
    then
         echo "X is greater than Y"
    elif [[ $X -eq $Y ]]
    then
         echo "X is equal to Y"
    fi
    

提交回复
热议问题