Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google site



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-10-2006
Registered User
 

Join Date: Feb 2006
Posts: 85
Problem in division

hi
I am having two variables namely a=7 & b=8. I have to subtract these two variables. I am using the command

c=`expr $a / $b`

When I check the value of c, it comes out to be zero.
Please help.
Regards
Rochit
Sponsored Links
  #2 (permalink)  
Old 08-10-2006
Registered User
 

Join Date: Jan 2005
Posts: 683
According to the MAN page:

Code:
     expr { \*, /, %} expr
           Multiplication,  division,   or   remainder   of   the
           integer-valued arguments.

what would you expect? Your answer will be zero. To get the remainder use the percent sign.

You can use awk:

Code:
 nawk 'BEGIN {print 7/8}'

  #3 (permalink)  
Old 08-10-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,200

Code:
quoting 'man expr':
     expr1 {*, /, %} expr2
             Return the results of multiplication, integer division, or
             remainder of integer-valued arguments.


Code:
a=7
b=8
echo "scale=2; $a / $b" | bc

  #4 (permalink)  
Old 08-10-2006
Registered User
 

Join Date: Feb 2006
Posts: 85
Dear tmarikle & vgersh99,
Thanks alot both of you for helping. both solutions are working
But it will be very nice of you vgesh99 if u could explain me the command
bc

Regards
Rochit
  #5 (permalink)  
Old 08-10-2006
Registered User
 

Join Date: Jan 2005
Posts: 683

Code:
nawk -v val_a=$a -v val_b=$b 'BEGIN {print val_a / val_b}'

or


Code:
nawk 'BEGIN {print '$a' / '$b'}'

  #6 (permalink)  
Old 08-10-2006
Registered User
 

Join Date: Feb 2006
Posts: 85
hi tmarkle
could u explain me your code

nawk -v val_a=$a -v val_b=$b 'BEGIN {print val_a / val_b}'

Thanks alot
Regards
Rochit
  #7 (permalink)  
Old 08-10-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,200
Quote:
Originally Posted by rochitsharma
Dear tmarikle & vgersh99,
Thanks alot both of you for helping. both solutions are working
But it will be very nice of you vgesh99 if u could explain me the command
bc

Regards
Rochit
take a quick look at 'man bc' - it should be self-explanatory.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Floating Division in Linux Dastard Shell Programming and Scripting 3 04-16-2008 01:57 PM
Round with division command vanloonmichel Shell Programming and Scripting 13 02-15-2007 09:32 AM
division in shell scripts esham Shell Programming and Scripting 6 11-30-2005 06:42 AM
Cannot adjust division jav_v Filesystems, Disks and Memory 1 08-20-2002 09:50 AM
division problem inquirer Shell Programming and Scripting 3 04-02-2002 11:03 PM



All times are GMT -4. The time now is 11:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0