在C++使用库中 setiosflags() functoin 的科学属性后,我如何才能将数字恢复到正常形式 #include "iomanip"

How in C++ can I get a number back to its normal form after using scientific property from setiosflags() functoin in #include "iomanip" library

本文关键字:数字 恢复 #include iomanip 常形式 setiosflags C++ 属性 functoin      更新时间:2023-10-16

我是在小写后在C 中编写代码。Instractor教给我们#include" iomanip"库的一部分的SetioSflags功能。他在我想要显示的数字上使用了科学属性。但是在我的情况下,它会打印出地址而不是科学数字,此后,当我使用固定属性作为同一函数时,数字不会再次转换回正常形式。它只是停留在科学属性的形式上

#include <iostream>
#include <climits>
#include "cmath"
#include "iomanip"
using namespace std;
int main() {
// a problem when I change it to scientific I could not get itback
cout<<"|"<<setw(20)<<142.1<<"|n";
cout<<"|"<<setw(20)<<setiosflags(ios::left)<<142.1<<"|n";
cout<<"|"<<setw(20)<<setiosflags(ios::scientific)<<142.1<<"|n";
cout<<"|"<<setw(20)<<setiosflags(ios::fixed)<<142.1<<"|n";
cout<<"|"<<setw(20)<<setiosflags(ios::right)<<142.1<<"|n";
return 0;
}

output:
|             142.100|
|142.100             |
|0x1.1c33333333333p+7|
|0x1.1c33333333333p+7|
|0x1.1c33333333333p+7|

您可以使用std::defaultfloat返回默认输出格式。

cout << "|" << setw(20) << std::defaultfloat << 142.1 << "|n";

请参阅https://ideone.com/yvslw4。

请参阅https://en.cppreference.com/w/cpp/io/manip/fixed有关该主题的更多信息。

setioSflags的对立面是重置。(科学和固定的标志没有链接/切换 - 它们是独立的(。

#include <iostream>
#include <climits>
#include "cmath"
#include "iomanip"
using namespace std;
int main() {
    // a problem when I change it to scientific I could not get itback
    cout<<"|"<<setw(20)<<142.1<<"|n";
    cout<<"|"<<setw(20)<<setiosflags(std::ios_base::left)<<142.1<<"|n";
    cout<<"|"<<setw(20)<<setiosflags(std::ios_base::scientific)<<142.1<<"|n";
    cout<<"|"<<setw(20)<<resetiosflags(std::ios_base::scientific)<<142.1<<"|n";
    cout<<"|"<<setw(20)<<setiosflags(std::ios_base::right)<<142.1<<"|n";
    return 0;
}

输出:

|               142.1|
|142.1               |
|1.421000e+02        |
|142.1               |
|               142.1|