可能重複失去1:
Why does this double to int conversion not work?轉換雙爲int在C + +
轉換雙爲int在C + +
#include <iostream>
#include <cmath>
using namespace std;
void main() {
double num = 1234.34;
int numInt = num;
double numAfterPoint = num - numInt; // 0.34
int counter = 1;
double numFloatPower = numAfterPoint;
while (true) {
numFloatPower = numAfterPoint * pow(10.0, counter);
cout << numFloatPower << " > " << (int)numFloatPower << " ";
system("pause");
counter++;
}
}
當前結果失去1:
3.4 > 3 Press any key to continue . . .
34 > 33 Press any key to continue . . .
340 > 339 Press any key to continue . . .
3400 > 3399 Press any key to continue . . .
的結果應該是:
3.4 > 3 Press any key to continue . . .
34 > 34 Press any key to continue . . .
340 > 340 Press any key to continue . . .
3400 > 3400 Press any key to continue . . .
等等
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Anycorn 2012-01-09 09:15:09
您能詳細說明一下這個問題,關於您期待什麼以及您得到什麼。 – 2012-01-09 09:16:36
那麼你的問題是什麼? – larsmoa 2012-01-09 09:16:37