-2

I might be missing something simple here, but when I divide two numbers that definately should not equal 0, I'm getting 0.

I've tried forcefully casting the values. SongDuration, originalSongDuration are integers. I need the percentage to be a float.

System.out.print("SongDuration: " + songDuration + " :: originalSongDuration: " + originalSongDuration + " :: percentage: " + (float) ((int)songDuration / (int)originalSongDuration));

[17:43:03 INFO]: SongDuration: 64746 :: originalSongDuration: 140746 :: percentage: 0.0

Thanks.

Toby Mellor
  • 7,498
  • 8
  • 31
  • 52

1 Answers1

1
(float) ((float)songDuration / (float)originalSongDuration));

If you want a percent you should use floats. Diving one int by another will never give a decimal value. You are probably getting 0 returned because of this property of ints

Derek Dawson
  • 374
  • 3
  • 12
  • 1
    FYI you don't need to cast both vars to float, just one will suffice. And as long as the variable where you're storing the result is a float, you don't need to cast the result. An alternative would be to multiply one of the vars by 1.0 – mstbaum Jun 10 '15 at 21:48