视频课程链接 P40 Output Formatting
05_9. Numeric Limits_哔哩哔哩_bilibili
当然这里面不全是C++20标准的,有些更早,反正比我学习的C++98新鲜的就都记下来了
欢迎批评指正交流!!!
#include <iostream>
#include <ios>
#include <iomanip>
using namespace std;
int main()
{
cout<<"----------"<<endl;
cout<<boolalpha; //将 0 1 转换成true false的形式
cout<<(11>5)<<endl;
cout<<"----double数字------"<<endl;
double num1=6.2381649;
cout<<setprecision(3)<<num1<<endl; //有效数字
cout<<fixed<<setprecision(3)<<num1<<endl; //fixed 小数 的 位数
cout.unsetf(ios::scientific|ios::fixed);
cout<<num1<<endl;//之前没有科学计数法,就用小数表示,延续上两行设定的3位有效数字
double num3=1.2357e+6;
cout<<num3<<endl; //之前是科学计数法表示的,继续用科学计数法,但是有效数字3位
cout<<"-----小数是否显示后面的0来补齐有效数字位数----" <<endl;
double num4=3.1;
cout<<"默认是不用显示的,这里延续上面setprecision=3的设定 "<<num4<<endl;
cout<<showpoint;
cout<<"现在显示后面的零了 "<<num4<<endl;
cout<<endl;
cout<<endl;
cout<<"----setw(n)通常用于处理多行文字,姓氏------"<<endl;
int wid=20;
cout<<setfill('-');
cout<<setw(wid)<<"First"<<setw(wid)<<"Last"<<setw(8)<<"age"<<endl;
cout<<setw(wid)<<"Bruce"<<setw(wid)<<"Liu"<<setw(8)<<"23"<<endl;
cout<<setw(wid)<<"Joke"<<setw(wid)<<"Smith"<<setw(8)<<"40"<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<"----默认右对齐,现在改为左对齐------"<<endl;
cout<<left;
cout<<setw(wid)<<"First"<<setw(wid)<<"Last"<<setw(8)<<"age"<<endl;
cout<<setw(wid)<<"Stando"<<setw(wid)<<"Kim"<<setw(8)<<"23"<<endl;
cout<<setw(wid)<<"Amy"<<setw(wid)<<"Janify"<<setw(8)<<"40"<<endl;
cout<<endl;
cout<<endl;
cout<<"----整数的各种进制 默认情况------"<<endl;
int num2=20934;
cout<<hex<<num2<<endl;
cout<<oct<<num2<<endl;
cout<<dec<<num2<<endl;
cout<<"----整数的各种进制 显示前缀------"<<endl;
cout<<showbase;
cout<<hex<<num2<<endl; //十六进制 0x开头
cout<<oct<<num2<<endl; //八进制 0 开头??javascript里面好像禁止使用八进制
cout<<dec<<num2<<endl;
return 0 ;
}