LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-14-2003, 02:33 AM   #1
causticmtl
Member
 
Registered: May 2003
Distribution: Slackware 10.0
Posts: 41

Rep: Reputation: 15
Bash, non-integers and arithmetic


Why is this happening?

$ let total+="4.5"; echo $total
-sh: let: total+=4.5: syntax error in expression (error token is ".5")
4

I need the variable to hold a non-integer. What's the workaround? I've read quite a few tutorials on the net concerning arithmetic and variables and the answer I keep getting is this one:

Quote:
if i ran "echo $[3/4]" at the command prompt, it would return 0 because bash only uses integers when answering. If you ran "echo 3/4|bc -l", it would properly return 0.75.
... yet if I do:

$ let total+=4.5|bc -l; echo $total

I get:

-sh: let: total+=4.5: syntax error in expression (error token is ".5")
4

What is the proper syntax?

Are there more comprehensive tutorials than the ones I have encountered?
 
Old 07-14-2003, 08:26 AM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
The problem is the definition of total. The code will need to echo 3/4 then pipe through bc -l, as:
Code:
total=`echo "3/4" | bc -l`
echo "total: $total"
which returns .75000000000000000000 on my system. (=
 
Old 07-15-2003, 12:15 AM   #3
causticmtl
Member
 
Registered: May 2003
Distribution: Slackware 10.0
Posts: 41

Original Poster
Rep: Reputation: 15
Thanks Linux Duck,

How would I get BASH to return only the first two digits after the decimal, essentially giving a result of .75? Is there a command which could restrict output to a "dollar" notation?

i.e. total=.75 or total=2.50 or total=21.25

Thanks again for your help.
 
Old 07-15-2003, 08:31 AM   #4
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Sure! There are several methods of doing so. One is to adjust the scale (number of decimal places that bc keeps). You could change the bc line to:
Code:
total=`echo "scale=2; 3/4" | bc -l`;
which returns
Code:
~/shell/casticmtl> ./math.sh
total is .75
That will solve it. Or, printf will do it, and make it a bit more friendly, as far as display money value goes. Change the echo to something like:

Code:
printf "total is \$%0.2g\n" $total
which outputs
Code:
~/shell/casticmtl> ./math.sh
total is $0.75
Just in case you're not familiar with the format, %g is the format for displaying a double, .2 will cause it to retain only the first two decimal places, and the 0 will cause it to be padded with zero's.

That should do it!!
 
Old 07-16-2003, 07:36 AM   #5
Mons
LQ Newbie
 
Registered: Jul 2003
Location: Kerala
Posts: 5

Rep: Reputation: 0
Hello Linux Duck:
I tried
rintf "total is \$%0.2f\n" $total:
The output is
total is $0.00
.75000000000000000000$
Could you please explain this?
Thanking you,
Mons...
 
Old 07-16-2003, 09:15 AM   #6
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
mons:

try changing the 'f' to 'g'. Also, can you post the script, just so I can see what it's doing?

TLD
 
  


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
64-bit integers in C nodger Programming 1 03-31-2005 08:06 AM
simple arithmetic in bash gfrair Linux - Newbie 9 03-16-2005 02:09 PM
converting integers to strings 1337 Twinkie Programming 12 08-06-2004 04:57 PM
Basic Java arithmetic question chr15t0 Programming 2 07-05-2003 01:32 PM
arithmetic operators in Kylix3 (C++) herbie_52 Programming 2 05-23-2003 07:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:02 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration