The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How can I get a processor's CPU Percentage? sangjinn High Level Programming 3 10-09-2008 08:28 AM
how to get the percentage of completion? during a copy! wrapster UNIX for Advanced & Expert Users 7 05-24-2008 01:55 AM
how to make percentage of a running script? jimmbp Shell Programming and Scripting 1 01-31-2006 02:41 PM
Computung Percentage preetikate UNIX for Dummies Questions & Answers 2 02-09-2004 10:13 AM
percentage sign in a drive mapping ? simon2000 UNIX for Advanced & Expert Users 2 09-17-2002 12:30 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-18-2007
the_learner the_learner is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 75
how do I calculate percentage ?

int percent (int a, int b)
{
if (b/a*100 > 25)
return TRUE;
else
return FALSE;
}

I want to calculate what percentage of a is b.

say if b = 48, a = 100
so b is 48% of a

but wouldnt b/a give me 0 ??? what can be done ??
  #2 (permalink)  
Old 04-18-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,100
percent=a*100/b
With integer arithemetic you must sequence this as (a*100)/b
  #3 (permalink)  
Old 04-18-2007
the_learner the_learner is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 75
u mean (b*100) /a ??

but then supposed (b*100)/a is 25.5 which is greater than 25. but still the condition (b/a*100 > 25) will evaluate to false right ?
  #4 (permalink)  
Old 04-18-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Use float
Code:
int percent (float  a, float  b)
Since you declared variables a and b as int , expression (b*100) /a is evaluated and rounded.
  #5 (permalink)  
Old 04-18-2007
the_learner the_learner is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 75
yes, yes.. I know, thats where the problem is, a and b are int
  #6 (permalink)  
Old 04-18-2007
niketan niketan is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 4
Why dont u store the division result in the float variable ... I guess compiler will auto cast to higher ... Just check with this

float f:

f= b/a * 100;

and then checking with 25 ...
  #7 (permalink)  
Old 04-18-2007
tantric tantric is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 23
since a and b are int b/a will return 0.
So, b/a * 100 will return 0. But since f is float, f will be 0.0

I think this works:

f = ((float)b * 100)/a
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 On



All times are GMT -4. The time now is 09:14 AM.


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

Content Relevant URLs by vBSEO 3.2.0