CEILING and FLOOR functions

Hi all,

Does anyone know how to simulate a ceiling or floor function in UNIX? OS is Solaris. I tried the suggestion from an old forum but it is giving me error as below:

server01[]/tmp$: echo "7.2" | awk '{printf("%d\n",$0+=$0<0?0:0.999)}'
awk: syntax error near line 1
awk: illegal statement near line 1

Am looking for something "simple", echo "7.123" | ceiling or echo "7.123" | floor that should return 8 or 7 respectively or something like that. Won't mind having to use awk i fneed be, but not sure how to call the function. I've also tried the one below but same error:

server01[]/tmp$: echo "7.2" | awk 'function ceil(valor) { return (valor == int(valor)) ? valor : int(valor)+1 } { printf "%d", ceil($1) }'
awk: syntax error near line 1
awk: bailing out near line 1

Any suggestion will be much appreciated. Thanks in advance.

You could use perl :

$ echo "7.2" | perl -nl -MPOSIX -e 'print ceil($_);'
8
$ echo "7.2" | perl -nl -MPOSIX -e 'print floor($_);'
7
$ 

floor

echo 7.2|cut -f1 -d"."

ceil

 
echo $(( `echo 7.2|cut -f1 -d"."` + 1 ))
1 Like

If you are on Solaris 10, you can also use /usr/bin/ksh93

$ /usr/bin/ksh93 -c 'print $(( ceil(7.2) ))'
8
$ /usr/bin/ksh93 -c 'print $(( floor(7.2) ))'
7
$