Operating System - HP-UX
1826073 Members
3523 Online
109690 Solutions
New Discussion

rounding numbers..my script function needs extension

 
SOLVED
Go to solution
amonamon
Regular Advisor

rounding numbers..my script function needs extension

I made function in ksh:

function tarif
{
a=$1
TarifToolConst=$2
b=`echo "($a+4)/5" | bc`
c=`echo "$b * 5" | bc`

#x=`echo "($a)/5" | bc`


f=`echo "scale=3;($b * $TarifToolConst) / 100" | bc`
g=`echo "(($b * $TarifToolConst) / 100)+1" | bc`
d=`echo "scale=3;($c * $TarifToolConst) / 100" | bc`
e=`echo "(($c * $TarifToolConst) / 100)+1" | bc`
echo "entry $a Rounded input $c Exit $d Rounded Exit $e and f is $f rounded price is $g USD"
#a=`expr $a + 1`
}

but I want if as input to this function I enter 10 this should be rounded to 15..in my example it is rounded to 10..and if I enter 15 it should be rounded to 20 if 20 should be rouned to 25..is this possible I have no idea..
In my script it already rounds input if I enter 4 to 5 if I enter 11 to 15 if I enter 16 to 20 ..etc..

any help or hint is appreciated..

Cheers,
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor

Re: rounding numbers..my script function needs extension

You can do all of this in one bc command. It is extremely inefficient to invoke one-line commands in the shell to execute a simple bc command. Your problem is that your bias (4) should be 5 although in almost all cases when rounding to the nearest 5, if I enter 10 then I would expect 10 as an output.

b=`echo "($a+4)/5" | bc`
c=`echo "$b * 5" | bc`

can be:

c=$(echo "((${a} + 5)/5) * 5")
If it ain't broke, I can fix that.
amonamon
Regular Advisor

Re: rounding numbers..my script function needs extension

nice hint..I love it..
I will change it!
But that is not releted to my problem..
Peter Godron
Honored Contributor
Solution

Re: rounding numbers..my script function needs extension

Hi,
so you want to round up to the nearest multiple of 5 ?

a=`echo "$1/5"|bc`
b=`expr $a + 1`
c=`echo "$b*5"|bc`

Input/output
4
a=0;b=1;c=5
5
a=1;b=2;c=10

Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.

A. Clay Stephenson
Acclaimed Contributor

Re: rounding numbers..my script function needs extension

Note that I not only changed your two statements into one but I also increased the bias to 5 which should do the "rounding" exactly as you wanted.

e.g.
((10 + 5)/5) * 5 = 15
((11 + 5)/5) * 5 = 15
((14 + 5)/5) * 5 = 15

Note that if you are doing interger math then you don't even need bc; you can do it in the shell.
If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: rounding numbers..my script function needs extension

>I want if as input to this function I enter 10 this should be rounded to 15

Didn't I already show something similar in your previous post:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1100461

>Clay: I also increased the bias to 5 which should do the "rounding" exactly as you wanted.

You could also truncate then add 1:
(( c = (a/5 + 1) * 5 ))

I'm not sure what you intended by this:
c=$(echo "((${a} + 5)/5) * 5")

That "*" is getting globbed.
Dennis Handly
Acclaimed Contributor

Re: rounding numbers..my script function needs extension

>I'm not sure what you intended by this:
c=$(echo "((${a} + 5)/5) * 5")

Perhaps you wanted this?
c=$(( (a + 5)/5 * 5 )) # spaces not needed
amonamon
Regular Advisor

Re: rounding numbers..my script function needs extension

thanks a lot guys