I was coding today, when I occurred this problem.
System.out.println(block.chance); // -> 10
float chanceFloat = block.chance / 100;
System.out.println(chanceFloat); // -> 0.0
I'm not sure what is the problem.
I was coding today, when I occurred this problem.
System.out.println(block.chance); // -> 10
float chanceFloat = block.chance / 100;
System.out.println(chanceFloat); // -> 0.0
I'm not sure what is the problem.
You are not stupid, you just are not aware of the language rules.
block.chance
seems to be an integer, and the result of a division of two integers is an integer, even if it is assigned to a float
cariable afterwards (then it is too late).
Your solution would be to use one of
float chanceFloat = block.chance / 100.0;
float chanceFloat = block.chance / 100.0f;
float chanceFloat = block.chance / 100.f;
float chanceFloat = block.chance / 100f;
float chanceFloat = (float)block.chance / 100;
If block.chance
is an integer, then block.chance/100
will be performed as integer division (i.e., the result is truncated), and then cast to a float.
If block.chance
is positive and less than 100, this will always be 0.
The simplest fix is to ensure that the divisions requires floating point numbers by changing the type of at least one side of the division. The common idiom would be
float result = block.chance / 100.0;