main()
{
double d1 = 1234.1;
cout << "d1 = 1234.1 --> " << d1 << endl;
double d2 = 1234.099999;
cout << "d2 = 1234.099999 --> " << d2 << endl;
}
輸出:如何保持雙的精度在C++
d1 = 1234.1 --> 1234.1
d2 = 1234.099999 --> 1234.1
其實我是想打印的d2
的精確值,即1234.099999
,但沒有得到相同的。
請建議我如何得到確切的價值。
你的代碼中有東西會丟失嗎?沒有什麼可以讓你在那裏得到d2。 – murrekatt 2011-03-04 07:43:31
你可能想看看這個線程:http://stackoverflow.com/questions/4217510/how-to-cout-the-correct-number-of-decimal-places-of-a-double-value – Chan 2011-03-04 09:20:40