Saltar al contenido

~ / linux / bash-round-up-floating-numbers

Bash: Round up floating numbers

linux · 1 min · bash, linux

I’ve found two easy ways to round up «ceiling» float numbers with bash.

Simply add «0.5» to your floating number and use printf:

sh
myfloating=$(echo "14.1129312938+0.5" | bc )
printf '%0.f' $myfloating
15

Or use awk:

sh
$myfloating=14.1
echo "$myfloating" | awk '{print ($0-int($0)>0)?int($0)+1:int($0)}
15

 

Dejá un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *