0

I wanna do this math in java:

int index = 3 * (9568/20001);

in my calculator it shows 3 *( 0.47837608... ) which is 1.43512824..

but, In Java that always give me 0, even I were trying use format, or java.lang.Math.round.

The first postion int 1 of 1.43512824 is what I want to get.

HCOOLH
  • 61
  • 9

1 Answers1

1

Try this

int index = (int)3 * (9568.0/20001);

Because an integer divided by an integer gives a integer in java thus your answer will not be accurate. If you write 9568.0/20001 it gives a double result and so result is more accurate.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Sourav Kanta
  • 2,727
  • 1
  • 18
  • 29