1826231 Members
2609 Online
109692 Solutions
New Discussion

rounding up numbers...

 
SOLVED
Go to solution
amonamon
Regular Advisor

rounding up numbers...

My problem lies in rounding numbers..

Let say I have as input 13 in script it should be rounded to 15 and multiplied with 9.5 and divided with 100
(or if I have as input 11 it should return 15, or for 19 it should be 20)

result is 1,425 but becouse I want this to be rounded final result should be 2.

can anyone give me a clue or help me with this..I tried something in KSH with

line this:

$echo "scale=2; 13 / 100" | bc


thanks in advance..
8 REPLIES 8
Peter Godron
Honored Contributor

Re: rounding up numbers...

Hi,
could you please provide a few more expected output values, given these input values:
0
4
5
6
9
10
11=15
14
15
16
19=20
20
21

If I understand correctly, round to the nearest multiple of 5 ?
amonamon
Regular Advisor

Re: rounding up numbers...

yes U are right..

0 =0
4 =5
5 =5
6 =10
9 =10
10=10
11=15
14=15
15=15
16=20
19=20
20=20
21=25

first round in input..and it is rounded as above..

thanks
Peter Godron
Honored Contributor

Re: rounding up numbers...

Hi,
how about:
#!/usr/bin/sh
a=0
while [ a -lt 30 ]
do
b=`echo "($a+4)/5" | bc`
c=`echo "$b * 5" | bc`
echo $a $b $c
a=`expr $a + 1`
done
amonamon
Regular Advisor

Re: rounding up numbers...

yes but I need to round up..just certain input..

like this:

Let say I have as input 13 in script it should be rounded to 15 and then multiplied with 9.5(constant) and then divided with 100
(or if I have as input 11 it should return 15, or for 19 it should be 20)

result is 1,425 but becouse I want this to be rounded final result should be 2.

thansks for useful hint!!
Peter Godron
Honored Contributor
Solution

Re: rounding up numbers...

Hi,
so, for
Input 13
Round up 13 to 15, then multiply 15 by 9.5 and devide by 100, resulting in 1.425. Then round this up to 2 ?

Can you please comment on results of:
#!/usr/bin/sh
a=0
while [ a -lt 30 ]
do
b=`echo "($a+4)/5" | bc`
c=`echo "$b * 5" | bc`
d=`echo "scale=3;($c * 9.5) / 100" | bc`
e=`echo "(($c * 9.5) / 100)+1" | bc`
echo "Input $a Rounded Input $c Output $d Rounded Output $e"
a=`expr $a + 1`
done
amonamon
Regular Advisor

Re: rounding up numbers...

that is it..Thanks..I just have to import this and to recreate it to fit in my main script

thanks..:)
Dennis Handly
Acclaimed Contributor

Re: rounding up numbers...

If your numbers are less than 2 billion, you can use shell arithmetic. From Peter's first example:
typeset -i a=0
while [ a -lt 30 ]; do
(( b = ((a+4)/5)*5 ))
echo "Input $a, Rounded Input $b"
(( a = a + 1 ))
done

If you want to change the number 5 above, that 4 is really (5-1).
amonamon
Regular Advisor

Re: rounding up numbers...

thanks for prompt assistence..

Cheers,