6
int cinema,dvd,pc,total;
double fractionCinema, fractionOther;
fractionCinema=(cinema/total)*100; //percent cinema

So when I run code to display fractionCinema, it just gives me zeros. If I change all the ints to doubles, then it gives me what Im looking for. However, I use cinema, pc, and total elsewhere and they have to be displayed as ints, not decimals. What do I do?

bitmoe
  • 391
  • 2
  • 5
  • 13
  • See [ Java Integer Division, How do you produce a double? ](http://stackoverflow.com/questions/3144610/java-integer-division-how-do-you-produce-a-double). – Matthew Flaschen Sep 15 '10 at 01:48
  • Nitpick from hard experience: don't call it "fractionCinema" when it is really "percentCinema". Makes support a year later very confusing! – user949300 Nov 01 '13 at 22:30

4 Answers4

16

When you divide two ints (eg, 2 / 3), Java performs an integer division, and truncates the decimal portion.
Therefore, 2 / 3 == 0.

You need to force Java to perform a double division by casting either operand to a double.

For example:

fractionCinema = (cinema / (double)total) * 100;
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • There is no need to force floating-point arithmetic, especially since OP says he doesn't need it in the final display. See my answer for an integer-only solution. – David R Tribble May 24 '13 at 15:12
5

Try this instead:

int  cinema, total;
int  fractionCinema;

fractionCinema = cinema*100 / total;   //percent cinema

For example, if cinema/(double)total is 0.5, then fractionCinema would be 50. And no floating-point operations are required; all of the math is done using integer arithmetic.

Addendum

As pointed out by @user949300, the code above rounds down to the nearest integer. To round the result "properly", use this:

fractionCinema = (cinema*100 + 50) / total;    //percent cinema
David R Tribble
  • 11,918
  • 5
  • 42
  • 52
  • Assuming the OP wanted only the integer values, this is the best answer. ("I use cinema, pc, and total elsewhere and they have to be _displayed_ _as_ _ints_, not decimals.") – qben May 23 '13 at 15:03
  • Minor drawback to this is that the result is always rounded down. Floating point solution is, IMO, better as it will round in the "normal" way. Also, nitpick, "fractionCinema" should be named "percentCinema". – user949300 Nov 01 '13 at 22:27
2

When you divide two ints, Java will do integer division, and the fractional part will be truncated.

You can either explicitly cast one of the arguments to a double via cinema/(double) total or implicitly using an operation such as cinema*1.0/total

NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • 2
    IMHO, the second example, `*1.0` which forces a multiplication merely to convert the type to double, is a code smell. Better to write code that clarifies your intention. "this needs to be converted to type double" is your intention. Use `(double)` -- that's what it is for. REASON: Someone else could easily come along, see a multiplication by 1.0, and think "that's a wasted multiplication, I'm going to remove it". You *could* add comment to the code line: `// "*1.0" is to convert to double`. But that would be silly, when the desired operator already exists. – ToolmakerSteve Jul 22 '14 at 01:10
0

As some people have already stated, Java will automatically cut off any fractional parts when doing division of integers. Just change the variables from int to double.