2017-03-14 124 views
0

這裏是我的代碼,只是一個基本的測試程序打印一個浮點數,printf它很好(我猜是因爲格式通過明確),但與std::cout我無法得到它的權利和std::setprecision似乎沒有幫助,你可以請給我一隻手?打印簡單的雙精度值與高精度

#include <iostream> 
#include <iomanip> 

int main() 
{ 
    std::cout << double(1999999900)/10000 << std::endl; 
    std::cout << std::setprecision(5) << double(1999999900)/10000 << std::endl; 
    printf("%f\n", double(1999999900)/10000); 
    return 0; 
} 

這裏是輸出,我怎樣才能從std::cout打印得到199999.99呢?

[[email protected] test00]# g++ -std=c++11 test1.cpp -o test1; ./test1 
200000 
2e+05 
199999.990000 
+1

實驗用[浮點I/O操縱器(http://en.cppreference.com/w/cpp/io/manip/fixed)。 –

+0

在一個不相關的說明中,你不需要強制轉換爲'double',只需使用像'1999999900.0'這樣的浮點數。 –

回答

2

使用

std::cout << std::setprecision(5) << std::fixed << double(1999999900)/10000 << std::endl;