Get Ceiling Integer from Number in Linux (Bash)

Get ceiling integer from number in linux (BASH)

Why use external script languages? You get floor by default. To get ceil, do

$ divide=8; by=3; (( result=(divide+by-1)/by )); echo $result
3
$ divide=9; by=3; (( result=(divide+by-1)/by )); echo $result
3
$ divide=10; by=3; (( result=(divide+by-1)/by )); echo $result
4
$ divide=11; by=3; (( result=(divide+by-1)/by )); echo $result
4
$ divide=12; by=3; (( result=(divide+by-1)/by )); echo $result
4
$ divide=13; by=3; (( result=(divide+by-1)/by )); echo $result
5
....

To take negative numbers into account you can beef it up a bit. Probably cleaner ways out there but for starters

$ divide=-10; by=10; neg=; if [ $divide -lt 0 ]; then (( divide=-divide )); neg=1; fi; (( result=(divide+by-1)/by )); if [ $neg ]; then (( result=-result )); fi; echo $result
-1

$ divide=10; by=10; neg=; if [ $divide -lt 0 ]; then (( divide=-divide )); neg=1; fi; (( result=(divide+by-1)/by )); if [ $neg ]; then (( result=-result )); fi; echo $result
1

(Edited to switch let ... to (( ... )).)

Bash Ceiling Function Rounding Down For Very Small Numbers

Use bc. It is a little robust, but works.

rounded_up=$(echo "scale=0; v=$shard_div; v=6; if (v%1 != 0) v = (v + 1); v / 1" | bc)

The / 1 is needed, otherwise bc doesn't scale. The v%1 == 0 case needs to be handled, otherwise whole number will be scaled up too (like v=5; 5+1 -> 6)

Ceil only floating point numbers in bash linux

you can use perl

NUMBER=365
perl -w -e "use POSIX; print ceil($NUMBER/1.0), qq{\n}"

for assigning to a variable

MYVAR=$(perl -w -e "use POSIX; print ceil($NUMBER/1.0), qq{\n}")

bash: How to round/floor/ceiling non-decimals thousands number?

The bash shell is able to do calculations internally, such as with the following transcript:

pax:~> for x in 100675 100499 100500 100999 101000; do
...:~> ((y = (x + 500) / 1000 * 1000))
...:~> echo " $x becomes $y"
...:~> done
100675 becomes 101000
100499 becomes 100000
100500 becomes 101000
100999 becomes 101000
101000 becomes 101000

This statement, ((y = (x + 500) / 1000 * 1000)), first adds 500 to make the otherwise-truncating integer division by 1,000 into a rounding division, then re-multiplies it by 1,000.

How do I perform this calculation and output it to standard out?

Although you ask to do this in Bash, there's no native support for functions like square root or ceiling. It would be simpler to delegate to Perl:

perl -wmPOSIX -e "print POSIX::ceil((sqrt(1 + 8 * $n) - 1) / 2)"

Alternatively, you could use bc to calculate the square root, and some Bash to calculate the ceiling.

Let's define a function that prints the result of the formula with sqrt of bc:

formula() {
local n=$1
bc -l <<< "(sqrt(1 + 8 * $n) - 1) / 2"
}

The -l flag changes the scale from the default 0 to 20.
This affects the scale in the display of floating point results.
For example, with the default zero, 10 / 3 would print just 3.
We need the floating point details in the next step to compute the ceiling.

ceil() {
local n=$1
local intpart=${n%%.*}
if [[ $n =~ \.00*$ ]]; then
echo $intpart
else
echo $((intpart + 1))
fi
}

The idea here is to extract the integer part,
and if the decimal part is all zeros, then we print simply the integer part,
otherwise the integer part + 1, as that is the ceiling.

And a final simple function that combines the above functions to get the result that you want:

compute() {
local n=$1
ceil $(formula $n)
}

And a checker function to test it:

check() {
local n num
for n; do
num=$(formula $n)
echo $n $num $(compute $n)
done
}

Let's try it:

check 1 2 3 4 7 11 12 16 17

It produces:

1 1.00000000000000000000 1
2 1.56155281280883027491 2
3 2.00000000000000000000 2
4 2.37228132326901432992 3
7 3.27491721763537484861 4
11 4.21699056602830190566 5
12 4.42442890089805236087 5
16 5.17890834580027361089 6
17 5.35234995535981255455 6

Round a divided number in Bash

To do rounding up in truncating arithmetic, simply add (denom-1) to the numerator.

Example, rounding down:

N/2
M/5
K/16

Example, rounding up:

(N+1)/2
(M+4)/5
(K+15)/16

To do round-to-nearest, add (denom/2) to the numerator (halves will round up):

(N+1)/2
(M+2)/5
(K+8)/16

round integer if output comes in decimal

You can use the modulo operator %:

#! /bin/bash
var1=126
var2=16
(( result = var1 / var2 ))
(( var1 % var2 && ++ result )) # If there's a remainder, add 1 to result.
echo $result

Convert floating point variable to integer?

Assuming $myduration is a decimal or integer

$ myduration=6.5
$ myduration=$( printf "%.0f" $myduration )
$ echo $myduration
6


Related Topics



Leave a reply



Submit