Operating System - HP-UX
1832931 Members
2825 Online
110048 Solutions
New Discussion

Re: rounding numbers after division

 
SOLVED
Go to solution
andi_1
Frequent Advisor

rounding numbers after division

Hi guys,

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!
6 REPLIES 6
MANOJ SRIVASTAVA
Honored Contributor

Re: rounding numbers after division

Hi Andi

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
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: rounding numbers after division

Hi:

Remember 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}"


If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: rounding numbers after division

Hi:

How about (for example):

# X=80;Y=14;echo "scale=1;($X/$Y)+0.5"|bc|awk -F. '{print $1}'

Regards!

...JRF...

Wodisch
Honored Contributor

Re: rounding numbers after division

Hi,

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
James R. Ferguson
Acclaimed Contributor

Re: rounding numbers after division

Hi (again):

This is a more direct [time to go home... ;)]:

# echo "80 14"|awk '{print int(($1/$2)+0.5)}'

Regards!

...JRF...

harry d brown jr
Honored Contributor

Re: rounding numbers after division

andi,

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
Live Free or Die