Bash script: specify bc output number format

前端 未结 6 1544
北荒
北荒 2020-12-03 15:26

Greetings!

I uses to make some calculations in my script. For example:

bc
scale=6
1/2
.500000

For further usage

6条回答
  •  星月不相逢
    2020-12-03 16:12

    I believe here is modified version of the function:

    float_scale=6
    
    function float_eval()
    {
        local stat=0
        local result=0.0
        if [[ $# -gt 0 ]]; then
            result=$(echo "scale=$float_scale; $*" | bc -q | awk '{printf "%f\n", $0}' 2>/dev/null)
            stat=$?
            if [[ $stat -eq 0  &&  -z "$result" ]]; then stat=1; fi
        fi
        echo $result
        return $stat
    }
    

提交回复
热议问题