Visit The New, Modern Unix Linux Community

Round off the a Decimal value.

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Round off the a Decimal value.
# 1  
Java Round off the a Decimal value.

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   /usr/sbin/prtconf|grep -i 'Memory size'|tr -s " "|/usr/xpg4/bin/awk -F" " '{print $3 * 1024}'`
v14=`ssh -l siebcl1   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.

Last edited by zaxxon; 10-21-2009 at 11:47 AM.. Reason: code tags
# 2  

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.

set +o nounset
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

echo " Variable V is \"$V\""

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

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

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

exit 0

% ./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"

 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
# 3  
integer expression expected

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"
# 4  
# printf "%.2f\n" 25.12345
# printf "%.2f\n" 25.12567

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #882
Difficulty: Easy
In October 2013 National Geographic released a file titled American Blackout which dealt with an imagined large-scale cyber attack on SCADA and the US electrical grid.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Decimal field round of

Hi, I want to round of decimal numbers in comma seperated lines till 2 decimal numbers. line will be like. ... (4 Replies)
Discussion started by: vishal0746
4 Replies

2. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

3. Shell Programming and Scripting

Help with Round Up with 2 decimal point at specific column

Input file: USA 20.5683 UK 3.54221 Japan 2.54001 China 2.50897 Germany 2.05816 . . Desired output file: USA 20.57 UK 3.54 Japan 2.54 China 2.51 Germany 2.06 . . (2 Replies)
Discussion started by: perl_beginner
2 Replies

4. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

5. UNIX for Dummies Questions & Answers

Convert hexa decimal to decimal

Hi, I want to convert two hexadecimal numbers to decimal using unix command line. 1cce446295197a9d6352f9f223a9b698 fc8f99ac06e88c4faf669cf366f60d I tried using `echo "ibase=16; $no |bc` printf '%x\n' "1cce446295197a9d6352f9f223a9b698" but it doesn't work for such big number it... (4 Replies)
Discussion started by: sudhakar T
4 Replies

6. Shell Programming and Scripting

How to round up value upto 2 decimal places using sed?

Please help me in rounding up value upto 2 decimal palces using sed command #!/usr/bin/bash a=15.42 b=13.33 c=`echo $a*$b |bc -l` echo $c above code is is giving output "205.5486" but i want the output as "205.55" Thank you... (15 Replies)
Discussion started by: ranabhavish
15 Replies

7. Homework & Coursework Questions

Decimal to BCD (Binary Coded Decimal)

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary... (2 Replies)
Discussion started by: caramba
2 Replies

8. UNIX for Dummies Questions & Answers

Decimal to BCD (Binary Coded Decimal)

Anybody please help me... Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary Coded Decimal) representation. Also, draw its Flow Chart. This is a unix qn... plz post algorithm for that :confused: (1 Reply)
Discussion started by: caramba
1 Replies

9. Shell Programming and Scripting

round decimal values and use in loops

i have a file in which 3 values are stored like --4.72 4.42 3.86 what i wanna do is that take read each value and compare with a fixed value say 5 but cant do so as i am getting an error for the same. please check the code cat /home/nsadm/auto/Logging/uptime.txt| while read a b c do if... (2 Replies)
Discussion started by: gemnian.g
2 Replies

Featured Tech Videos