开发者

Confused with floats and ints

开发者 https://www.devze.com 2023-02-27 03:05 出处:网络
They\'re not the best of friends. I have this: 开发者_如何学编程int svNumberOfObjects = 9; and this:

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

精彩评论

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