- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: rounding numbers after division
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2002 12:20 PM
05-07-2002 12:20 PM
Sometimes, I need to divide values, and shell will return zero if the result is less then 1.
e.g.
let tmp=10/100
echo $tmp # will display 0
How can I make sure that I can round-up the result to the next value.
e.g.
10/100 = 1
11/100 = 2
Thank you!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2002 12:35 PM
05-07-2002 12:35 PM
Re: rounding numbers after division
You may like to set the scale in bc to the decimal by setting like scale=2 and then doing the calculation. Please look into man paages for bc and dc.
Manoj Srivastava
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2002 12:48 PM
05-07-2002 12:48 PM
SolutionRemember that arithmetic with the shell is integer only. The best way to do this is with a function into which you pass a value and a divosor (if you want to do this all within the shell). I trust that you meant that both 10/100 and 11/100 should 'round up' to 1 because otherwise you have invented a new type of math.
Something like this should be close:
#!/usr/bin/sh
divide_and_roundup()
{
STAT=0
if [ $# -lt 2 ]
then
echo "Function divide_and_roundup requires 2 args" >&2
return 255
fi
A=$1
shift
B=$1
shift
if [ ${B} -eq 0 ]
then
echo "Function divide_and_roundup divide by zero!!!" >&2
return 254
fi
C=$(( ${A} / ${B}))
if [ $((${A} % ${B})) -ne 0 ]
then
if [ ${C} -ge 0 ]
then
C=$((${C} + 1))
else
C=$((${C} - 1))
fi
fi
echo "${C}\n"
return ${STAT}
} # divide_and_roundup
# ---------------
X=10
Y=100
Z=$(divide_and_roundup ${X} ${Y})
echo "X = ${X} Y = ${Y} Z = ${Z}"
X=102
Y=100
Z=$(divide_and_roundup ${X} ${Y})
echo "X = ${X} Y = ${Y} Z = ${Z}"
X=100
Y=100
Z=$(divide_and_roundup ${X} ${Y})
echo "X = ${X} Y = ${Y} Z = ${Z}"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2002 12:56 PM
05-07-2002 12:56 PM
Re: rounding numbers after division
How about (for example):
# X=80;Y=14;echo "scale=1;($X/$Y)+0.5"|bc|awk -F. '{print $1}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2002 01:07 PM
05-07-2002 01:07 PM
Re: rounding numbers after division
well, shell isn't the best suited tool for arithmetic...
Why not use "awk" or "perl" or "tcl" for this?
Much easier, not slower, better precision, and all freely available ("awk" is even there, already).
Just my $0.02,
Wodisch
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2002 02:48 PM
05-07-2002 02:48 PM
Re: rounding numbers after division
This is a more direct [time to go home... ;)]:
# echo "80 14"|awk '{print int(($1/$2)+0.5)}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2002 06:25 PM
05-07-2002 06:25 PM
Re: rounding numbers after division
let me teach you something about when politican's get involved with education:
Location: NY
Political body: NY Board of Regents
Rounding rules:
when decimal portion being rounded up is a "5" and the number being rounded up results in an odd number, then rounding is not to occur - truncation occurs:
1.50 = 2.00
2.50 # 3.00 = 2.00
3.50 = 4.00
1.05 = 1.00
1.15 = 1.20
1.25 = 1.20
1.005 = 1.00
1.015 = 1.02
1.025 = 1.02
1.035 = 1.03
And I'm not kidding about this. Sad, very sad!
live free or die
harry