Java Dividing Two Floats Getting 0
I am trying to divide two integers values to get a float value.. and I always get the value 0.0.. already tried to cast the values to float and no chance anyway, here is the code:
float othersFloat = (float) others; float totalPixelsFloat = (float) totalPixels; // this variables have the values: // othersFloat : 621347.0 // totalPixelsFloat : 654336.0 // then I do the divison like this: float percentage_white_on_screen = (float) othersFloat / totalPixelsFloat; //But I get the value 0.0
Can someone help me?
There is nothing wrong with the code you have shown us. If the inputs are given by the comments, then the result should not be 0.0.
So if it is then either:
the actual inputs do not have the values that you think that they do,
the actual output value is different to what you think it is; e.g. you are printing a different variable ... or something after this code is changing it, or
that is not the code you are executing it; e.g. you've not recompiled it and the code you are running no longer matches the source code.
If this does not help you find the real cause of the problem, you will need to write an SSCCE ... so that other people can actually reproduce your problem for themselves.