问题描述
这可能与此问题重复,但是我觉得它实际上没有正确回答.观察:
This may be a duplicate of this question, but I don't feel it was actually answered correctly. Observe:
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
float p = 1.00;
cout << showpoint << setprecision(3) << p << endl;
}
输出: 1.00
现在,如果我们将该行更改为:
Now if we change that line to:
cout << fixed << showpoint << setprecision(3) << p << endl;
我们得到: 1.000
如果我们使用固定的对立面",我们会得到完全不同的东西:
And if we use the "opposite" of fixed we get something totally different:
cout << scientific << showpoint << setprecision(3) << p << endl;
输出: 1.000e + 00
设置了固定
后,如何返回第一个版本的行为?
How can I go back to the behaviour of the first version after fixed
has been set?
推荐答案
浮点的格式规范是位掩码调用 std :: ios_base :: floatfield
.在C ++ 03中,它具有两个命名设置( std :: ios_base :: fixed
和 std :: ios_base :: scientific
).默认设置是不设置这些标志.例如,可以使用
The format specification for floating points is a bitmask call std::ios_base::floatfield
. In C++03 it has two named settings (std::ios_base::fixed
and std::ios_base::scientific
). The default setting is to have neither of these flags set. This can be achieved, e.g., using
stream.setf(std::ios_base::fmtflags(), std::ios_base::floatfield);
或
stream.unsetf(std::ios_base::floatfield);
(字段类型为 std :: ios_base :: fmtflags
).
使用当前的C ++,还有 std :: ios_base :: hexfloat
,并且添加了两个操纵器,特别是 std :: defaultfloat()
可以清除 std :: ios_base :: floatfield
:
With the current C++ there is also std::ios_base::hexfloat
and there are two manipulators added, in particular std::defaultfloat()
which clears the std::ios_base::floatfield
:
stream << std::defaultfloat;
这篇关于确实,“固定"广告与“固定"广告相反.输入/输出操纵器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!