LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bourne and Decimals? (https://www.linuxquestions.org/questions/programming-9/bourne-and-decimals-640426/)

kdyzsa 05-06-2008 10:02 PM

Bourne and Decimals?
 
I'm suppose to use the Bourne Shell

I need to get 15% of the variable exer1 to be added to other exercises

so far, i've got
exer1=$1
aver=`expr $exer \* .15`

but i keep getting an error that an integer value was expected. Is there anyway around this?

eggixyz 05-06-2008 10:30 PM

Hey there,

The straight Bourne shell doesn't have any support for floating point decimals.

The usual work-around is to (on a hundredth, like .15 or .87, etc) move the decimal point over two right and do the math, then move it back over 2 left.

Chek this link out for a script that deals with floating point in ksh (but also works in Bourne - the arithmetic, anyway):


http://linuxshellaccount.blogspot.co...metic-and.html

Hope that helps,

Mike

kdyzsa 05-07-2008 12:13 AM

Hm..
 
Well.. unfortunately the site can't be opened since it's blocked here.
So I thought about that idea so I'll cut it.
ex.

exer=70
aver=`expr $exer \* 15`

the sample output would be aver =1050, but unfortunately cut doesn't work with integers i think since i tried first=`echo $aver | cut -c1-2` and the output i got was "an integer value was expected" Any help guys?

chrism01 05-07-2008 12:37 AM

Try bc

echo 70*15|bc
1050

echo 70*.15|bc
10.50

http://www.faqs.org/docs/abs/HTML/mathc.html

kdyzsa 05-07-2008 01:16 AM

Thanks
 
Thanks a lot for the bc tip. It's working smoothly now. I was stuck at that problem a while ago. Now I'll to finish my script by the end of the day. Thanks again ;)


All times are GMT -5. The time now is 08:36 AM.