Round off the a Decimal value.

HI,
I have a script which is used to calculate the Memory & CPU utilization a server.

memx=`ssh -l siebel1 ${f}   /usr/sbin/prtconf|grep -i 'Memory size'|tr -s " "|/usr/xpg4/bin/awk -F" " '{print $3 * 1024}'`
v5=`ssh -l siebel1 ${f}   vmstat 1 2 | tail -1 | tr -s " " | /usr/xpg4/bin/awk -v mm=${memx} '{ x=mm-$5; y=x/mm ; print y*100}'`

This is giving me an output in percentage upto 4 decimal - ie - 91.4589
But i want this should show an output as 91.46 or atleast 91

I tried using floor cmd but its not working here.

memx=`ssh -l siebcl1 147.149.126.116   /usr/sbin/prtconf|grep -i 'Memory size'|tr -s " "|/usr/xpg4/bin/awk -F" " '{print $3 * 1024}'`
v14=`ssh -l siebcl1 147.149.126.116   vmstat 1 2 | tail -1 | tr -s " " | /usr/xpg4/bin/awk -v mm=${memx} '{ x=mm-$5; y=x/mm ; print y*100}'`
v5=print $((floor(v14)))

ERROR - floor(v14): syntax error in expression (error token is "(v14)")

Can you suggest some small method by which i can round it off over there itself or without changing much.

Hi.

Probably easiest to use awk printf, but there is also a command printf:

#!/usr/bin/env bash
#!/usr/bin/env zsh
#!/usr/bin/env ksh

# @(#) s1	Demonstrate control of precision in floating-point display.

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) awk /usr/bin/printf printf
set -o nounset

V="91.4589"
echo
echo " Variable V is \"$V\""

echo
echo " awk:"
awk -v value="$V" '
END	{ printf(" 2 places: %.2f\n", value)
      printf(" 0 places: %.0f\n", value) }
' /dev/null

echo
echo " Command /usr/bin/printf:"
/usr/bin/printf " 2 places: %.2f\n" "$V"

echo
echo " builtin command printf:"
builtin printf " 2 places: %.2f\n" "$V"

exit 0

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
GNU Awk 3.1.5
/usr/bin/printf printf (GNU coreutils) 6.10
printf - is a shell builtin [bash]

 Variable V is "91.4589"

 awk:
 2 places: 91.46
 0 places: 91

 Command /usr/bin/printf:
 2 places: 91.46

 builtin command printf:
 2 places: 91.46

The extra shebangs are for testing, move them to the top if you wish to run a different shell.

See man awk or man printf for details on printf formatting.

The builtin didn't work on the version of ksh that I use, but it did with zsh.

Best wishes ... cheers, drl

Hi,, Thanks for the suggestion but,,, the output from printf is in char. & we need it in integer format.
Due to which this is failing .....
"[: 25.00: integer expression expected"

# printf "%.2f\n" 25.12345
25.12
# printf "%.2f\n" 25.12567
25.13