TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Miles Parfitt
from: David Nugent
date: 1995-02-11 18:16:30
subject: ARGHHHHHHHHHHHHHHHH!

> Would someone please tell me what the hell is wrong with this
 > code.  All it gives me is 0 when it should be a percentage
 > value.

The division you're attemping is integral.

 > The two values total and avail are long type integers and
 > total is larger than avail.

 >        float per_used = (avail / total) * 100;

The result of ( avail / total ) is obviously less than 1, and it will
result in an integral result (0) rather than a float. Already you've
truncated your result.

Try:

    float( avail ) / float( total ) * 100

or:

    ( avail * 100 ) / total

Note that in the second case, you won't yield any decimal places in the
resulting percentage. If you want those to reflect reality, use the first
method to use floating point division rather than integral division.


david

---
* Origin: Unique Computing, Melbourne, Australia (3:632/348)
SEEN-BY: 50/99 54/54 620/243 623/630 624/50 632/103 301 348 386 998 633/371
SEEN-BY: 633/379 634/384 635/301 502 503 541 544 636/100 639/100 711/401 409
SEEN-BY: 711/410 430 510 807 808 809 932 934 942 712/515 713/888 714/906
SEEN-BY: 800/1 7877/2809
@PATH: 632/103 348 635/503 50/99 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.