Shell – Convert a Float to the Next Integer Up

floating pointshell

The context is there are 2 variables that get divided to a floating point result like so:

printf "%0.5f\n" $(echo 305/15 | bc -l)
20.33333

How can I always round up to the next integer i.e. 21? This is not about rounding up a value above 20.5 to 21 i.e. nearest integer. I'm asking because I want a value to be either exactly the integer or the next integer if it's above in whatever way. So how can I evaluate that? With an if statement? If I put a float there the shell complains it expects an integer. I don't fully understand how to leverage the information in a Q&A such as this one to effect a conversion "upward" to the next integer. Something I'm missing?

Best Answer

You can use bc features for that:

echo "a=305; b=15; if ( a%b ) a/b+1 else a/b" | bc