在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
我是在小写后在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|
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 用C++将哈希表写入文件并从文件中恢复
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 如何检查一个c++字符串中有多少相同的字符/数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 将数字打印成文字
- 在C++使用库中 setiosflags() functoin 的科学属性后,我如何才能将数字恢复到正常形式 #include "iomanip"