Get ceiling integer from number in linux (BASH)

后端 未结 13 1981
陌清茗
陌清茗 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

    Mathematically, the function of ceiling can be define with floor, ceiling(x) = -floor(-x). And, floor is the default when converting a positive float to integer.

    if [ $N -gt 0 ]; then expr 1 - $(expr $(expr 1 - $N) / 500); else expr $N / 500; fi
    

    Ref. https://en.wikipedia.org/wiki/Floor_and_ceiling_functions

提交回复
热议问题