开发者

Why can't I output a basic double in c++ [duplicate]

This question already has answers here: Closed 12 years ago.
开发者_如何转开发

Possible Duplicate:

Incorrect floating point math?

Using C++ console app in visual studio.

 float percentCorrect;
 percentCorrect = ( 7 / 5 );
 printf("%f", percentCorrect);

output is 1.0000

OK so how do I get the correct decimal output??


7 and 5 are integers, so the compiler treats them as such and does integer division.

float percentCorrect; 
percentCorrect = (7.0/5.0); 
printf("%f", percentCorrect);

The above should print out the expected value.


You need to include a decimal: 7.0/5.0


7 and 5 are integer literals, so 7 / 5 performs integer division.

You need to have at least one floating literal for floating-point division to be performed:

float percentCorrect = 7.0 / 5.0;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜