Get ceiling integer from number in linux (BASH)

后端 未结 13 1990
陌清茗
陌清茗 2020-12-01 04:54

How would I do something like:

ceiling(N/500)

N representing a number.

But in a linux Bash script

13条回答
  •  感情败类
    2020-12-01 05:37

    This function wont't add 1, if the division returns a non-floating number.

    function ceiling {
        DIVIDEND=${1}
        DIVISOR=${2}
        if [ $(( DIVIDEND % DIVISOR )) -gt 0 ]; then
                RESULT=$(( ( ( $DIVIDEND - ( $DIVIDEND % $DIVISOR ) ) / $DIVISOR ) + 1 ))
        else
                RESULT=$(( $DIVIDEND / $DIVISOR ))
        fi
        echo $RESULT
    }
    

    Use it like this:

    echo $( ceiling 100 33 )
    > 4
    

提交回复
热议问题