C++流操纵算子

整型数

cout << y;
//整型数
int n = 10;
cout << n << endl; //10进制,默认
cout << hex << endl; //十六进制输出
cout << dec << endl; //10进制
cout << oct << endl; //十八进制

浮点数

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
  double x = 1234567.89,y = 12.34567;
  int n = 12;
   int m = 1234567
cout << setprecision(6) << x << endl << y << endl << n << endl << m;//截断精度是针对浮点数的,保留6位有效数字。默认也是保留6位。
//输出就是 123457,12.3457,12 1234567

}
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
  double x = 1234567.89,y = 12.34567;
  int n = 12;
   int m = 1234567
cout << setiosflags(ios::fixed) << setprecision(6) << x << endl << y << endl << n << endl << m;//设定浮点数必须输出为小数形式,此时精度就变成了小数点后要保留的有效数字位数。
//输出就是 1234567.890000,12.345670,12,1234567

}

设定域宽

cin.width(5);//设置第一次输入的长度限制(4个字符),从输入中截取
cin >> string;//假如输入为:123456789
cout << string << endl; // 1234
cin >> string;
cout << string << endl; //567890

猜你喜欢

转载自www.cnblogs.com/Henry-ZHAO/p/12907973.html