防止在C ++中显示小数点前的零

时间:2013-10-06 21:53:34

标签: c++ output setf

在C ++中,我使用以下语句来显示输出:

// example
float x = 0.66004;
cout.setf(ios::fixed);  
cout.setf(ios::showpoint);  
cout.precision(2);
cout << x;

我的输出看起来像

0.66

如何防止小数点前的零显示?我想要:

.66

2 个答案:

答案 0 :(得分:0)

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);
}

答案 1 :(得分:0)

一种可能的解决方案是将x转换为字符串(more选项)并切断小数点前的所有内容。