user2007843
user2007843

Reputation: 609

printing the percent completed

I am trying to divide 2 Long's in order to find the percentage. However, I only get 0.0% or when they are equal, 100.0%. This is what I am doing

long size =
long mem = 

double avg = (double)(size / mem) * 100;

Upvotes: 0

Views: 101

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533500

If you want to avoid long decimal numbers you can use.

long percentage = 100 * size / mem; // as an integer.

If you want one decimal place you can do

double percentage = (1000 * size / mem) / 10.0;

Upvotes: 1

Reimeus
Reimeus

Reputation: 159754

You're doing long division which truncates the fractional part of the number. At least one of the operands is required to be a double to produce a non-zero double, You could do:

double avg = (double)size / mem * 100;

The numerator now becomes a double as a cast has high precedence than the division operator /.

Upvotes: 5

Related Questions