LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-06-2008, 10:02 PM   #1
kdyzsa
LQ Newbie
 
Registered: May 2008
Posts: 7

Rep: Reputation: 0
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?
 
Old 05-06-2008, 10:30 PM   #2
eggixyz
Member
 
Registered: Apr 2008
Posts: 310

Rep: Reputation: 30
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
 
Old 05-07-2008, 12:13 AM   #3
kdyzsa
LQ Newbie
 
Registered: May 2008
Posts: 7

Original Poster
Rep: Reputation: 0
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?
 
Old 05-07-2008, 12:37 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,241

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
Try bc

echo 70*15|bc
1050

echo 70*.15|bc
10.50

http://www.faqs.org/docs/abs/HTML/mathc.html
 
Old 05-07-2008, 01:16 AM   #5
kdyzsa
LQ Newbie
 
Registered: May 2008
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
use decimals in java? rabbit2345 Programming 1 03-08-2008 12:08 AM
how to compare decimals linuxboy69 Linux - Software 2 08-16-2004 02:43 PM
Can I use a lot of decimals in my C program? Golradir Programming 14 07-04-2004 12:40 PM
bourne vs bash alaios Linux - Software 2 03-13-2004 12:16 AM
Bourne Again SHell help MagInnovision Linux - Newbie 2 02-04-2003 03:43 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration