开发者

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜