Confused with floats and ints
They're not the best of friends.
I have this:
开发者_如何学编程int svNumberOfObjects = 9;
and this:
int svColumns = 4;
and then there's also this:
float numberOfRows = svNumberOfObjects/svColumns;
which comes out with this:
2.000000
How can i get it to show me the actual float value?
float numberOfRows = (float)svNumberOfObjects/svColumns;
Your code at present uses integer division, which truncates the result to an integer. You want a floating-point division instead, which means that you need one of the operands to be a floating-point value; the explicit cast accomplishes this.
float numberOfRows = (float)svNumberOfObjects/svColumns;
You can also try
float numberOfRows = 1.0*svNumberOfObjects/svColumns;
精彩评论