[英]How to convert a floating point number into a string with precision? Arduino C++
[英]How to print floating point number in C++ similar to C
我想以pdb格式打印數據,這是一種存儲原子坐標的特定格式,以便可以由某些標准分子可視化軟件讀取。
目前,我正在使用常規C ++進行工作,並將std :: cout與printf結合使用,以獲得所需的格式化輸出,如下所示:
std::cout << std::setw(6) << "ATOM" << std::setw(5) << "0" << " "
<< std::setw(4) << "C" << std::setw(12) << global_id
<< " ";
printf("%8.3f %8.3f %8.3f %6.2f %6.2f \n", pos[0], pos[1], pos[2], tt, ss );
其中global_id是整數。
因此,如何僅使用小數點前后的指定精度的std :: cout消除此printf並編寫整個語句。
任何幫助將不勝感激。
您正在尋找的機制是setprecision()
如下所述: http : //en.cppreference.com/w/cpp/io/manip/setprecision
與setw()
結合使用:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.