Stringstream Noshowpoint 不起作用

stringstream noshowpoint doesn't work

本文关键字:不起作用 Noshowpoint Stringstream      更新时间:2023-10-16

我正试图将67.5写为006750,代码如下:

 float price = 67.5
 stringstream symbol;
 symbol << setfill('0') << setw(6) << fixed << setprecision(2) 
        << noshowpoint << price;

但输出为067.50

您感到困惑。std::noshowpoint只消除整数浮点上的尾随.0,例如60.0输出为60,它并不是简单地去除所有数字上的点。

要得到你想要的,你可以这样做:

float price = 67.5;
std::stringstream symbol;
symbol << std::setfill('0') << std::setw(6) << int(100 * price);

看起来你正试图使用noshowpoint来删除小数,但如果你看看这里的例子,你会发现当pi用noshowpoint打印时,它仍然保留小数,可能是因为没有它你会失去精度。

我可能只是在这里手动操作一些字符串,因为我看到的字符串流选项似乎都不能满足您的要求。