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?

Answers


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.


Need Your Help

R - How to overlay the average of a set of iid RVs

r ggplot2 statistics

In the code below I build a 40x1000 data frame where in each column I have the cumulative means for successive random draws from an exponential distribution with parameter lambda = 0.2.

Stopping a Thread

c# multithreading

Here is the situation. I have a thread that gets started with the click of a button. It then works for approx 2 min (give or take quite a bit) and then finishes, updating the UI and all along the...