1
在C++中的小數點前的零,我用下面的語句來顯示輸出:防止顯示在C++
// example
float x = 0.66004;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << x;
我的輸出看起來像
0.66
如何防止顯示小數點前的零點?我想:
.66
在C++中的小數點前的零,我用下面的語句來顯示輸出:防止顯示在C++
// example
float x = 0.66004;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << x;
我的輸出看起來像
0.66
如何防止顯示小數點前的零點?我想:
.66
std::string Foo = std::to_string(0.553);
std::size_t it = Foo.find(".");
if (it != std::string::npos)
{
std::cout<<Foo.erase(0, it);
}
一個可能的解決方案是將x
串(more選項),並切斷小數點前的一切。
謝謝!我沒有想過使用字符串。認爲這可能是某種setf修飾符。 – Elliott