我嘗試以下的代碼:顯式類型鑄造浮子(intvar)導致INT
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int intvar = 25;
float floatvar = 35.87;
cout << "intvar= " << intvar;
cout << "\n floatvar =" << floatvar;
cout << "\n float(intvar)=" << float(intvar);
cout << "\n int(floatvar)=" << int(floatvar);
_getch();
return 0;
}
爲float(intvar)
結果來作爲25
。
有人可以解釋爲什麼它仍然顯示爲一個整數而不是25.000000
?
因爲它們的意思是一樣的嗎?你究竟如何選擇零的數量?無論如何,這可能會有所幫助,如果你想格式化輸出:http://stackoverflow.com/questions/11989374/floating-point-format-for-stdostream/11989420#11989420 –