2013-05-08 66 views
2

我目前使用固定小數精度

std::cout.precision(5); 

設置我的輸出的小數精度。但是,我寧願讓我的 輸出總是輸出5個小數位(現在它不會顯示0)。 我會如何更改我的代碼以反映這一點?

回答

0

寫一個println函數,它接受一個浮點,並用它來代替COUT的,在內部調用COUT精確

8

您與std::setprecision一起尋找std::fixed

#include <iomanip> 
#include <iostream> 
double f =1.1; 
std::cout << std::fixed; 
std::cout << std::setprecision(5) << f << std::endl; 

標準輸出

1.10000 
3

嘗試:

std::cout.precision(5); 
std::cout << std::fixed; 
std::cout << a << std::endl; //output a with fixed precision 5 

在這裏看到:std::fixed一些例子。

+0

完美!謝謝。 – user2105982 2013-05-08 14:36:45

+0

@ user2105982歡迎您 – taocp 2013-05-08 14:38:00

0

是很方便的事:

std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield); 
std::cout.precision(x);