[转载] c++ cout 格式化输出浮点数、整数及格方法
C语言里可以用printf(),%f来实现浮点数的格式化输出,用cout呢。。。? 下面的方法是在网上找到的,如果各位有别的办法谢谢留下...iomanip.h是I/O流控制头文件,就像C里面的格式化输出一样.以下是一些常的:dec 置基数为10 相当于"%d" hex 置基数为16 相当于"%X" oct 置基数为8 相当于"%o" setfill(c) 设填充字符为c setprecision(n) 设显示小数精度为n位 setw(n) 设域宽为n个字符 setioflags(ios::fixed) 固定的浮点显示 setioflags(ios::scientific) 指数表示 setiosflags(ios::left) 左对齐 setiosflags(ios::right) 右对齐 setiosflags(ios::skipws 忽略前导空白 setiosflags(ios::uppercase) 16进制数大写输出 setiosflags(ios::lowercase) 16进制小写输出 setiosflags(ios::showpoint) 强制显示小数点 setiosflags(ios::showpos) 强制显示符号#include <iomanip> use namespace std;double d=11.23456; cout<<d<<endl; //直接输出只能输出6位数,包括整数部分和小数部分 cout<<setprecision(3)<<d<<endl; //精度为3,输出3位数 cout<<setiosflags(ios::fixed)<<d<<endl;//精度为3,定点输出,输出3位小数 cout<<setiosflags(ios::fixed)<<setprecision(7)<<d<<endl;//位数不够,末尾添0 输出结果: 11.2346 11.2 11.23456 11.2345600C++格式化输出浮点数view plaincopy to clipboardprint? 01.#include <iostream> 02.using std::cout; 03.using std::endl; 04.using std::fixed; 05.using std::scientific; 06. 07.int main() 08.{ 09. double x = 0.001234567; 10. double y = 1.946e9; 11. 12. cout << "Displayed in default format:" << endl << x << '\t' << y << endl; 13. 14. cout << "\nDisplayed in scientific format:" << endl << scientific << x << '\t' << y << endl; 15. 16. cout << "\nDisplayed in fixed format:" << endl << fixed << x << '\t' << y << endl; 17. return 0; 18.} #include <iostream> using std::cout; using std::endl; using std::fixed; using std::scientific;int main() {double x = 0.001234567;double y = 1.946e9;cout << "Displayed in default format:" << endl << x << '\t' << y << endl;cout << "\nDisplayed in scientific format:" << endl << scientific << x << '\t' << y << endl;cout << "\nDisplayed in fixed format:" << endl << fixed << x << '\t' << y << endl;return 0; }Displayed in default format: 0.00123457 1.946e+009Displayed in scientific format: 1.234567e-003 1.946000e+009Displayed in fixed format: 0.001235 1946000000.000000view plaincopy to clipboardprint? 01.#include <iostream.h> 02. 03.main(void) 04.{ 05. float a=100100.0, b=0.08; 06. cout.setf(ios::right|ios::scientific|ios::showpoint); 07. cout.width(20); 08. cout <<(-a*b); 09. 10. return 0; 11.} #include <iostream.h>main(void) {float a=100100.0, b=0.08;cout.setf(ios::right|ios::scientific|ios::showpoint);cout.width(20); cout <<(-a*b);return 0; }-8.008000e+003view plaincopy to clipboardprint? 01.#include <iostream> 02.#include <iomanip> 03.#include <limits> 04.using std::cout; 05.using std::endl; 06.using std::setprecision; 07.using std::numeric_limits; 08. 09.int main() { 10. const double pi = 3.14; 11. cout << endl; 12. 13. for(double radius = .2 ; radius <= 3.0 ; radius += .2) 14. cout << "radius = " 15. << setprecision(numeric_limits<double>::digits10 + 1) 16. << std::scientific << radius<< " area = " 17. << std::setw(10) << setprecision(6)<< std::fixed << pi * radius * radi 18.us << endl; 19. return 0; 20.} #include <iostream> #include <iomanip> #include <limits> using std::cout; using std::endl; using std::setprecision; using std::numeric_limits;int main() {const double pi = 3.14;cout << endl;for(double radius = .2 ; radius <= 3.0 ; radius += .2)cout << "radius = "<< setprecision(numeric_limits<double>::digits10 + 1)<< std::scientific << radius<< " area = "<< std::setw(10) << setprecision(6)<< std::fixed << pi * radius * radi us << endl;return 0; }radius = 2.0000000000000001e-001 area = 0.125600 radius = 4.0000000000000002e-001 area = 0.502400 radius = 6.0000000000000009e-001 area = 1.130400 radius = 8.0000000000000004e-001 area = 2.009600 radius = 1.0000000000000000e+000 area = 3.140000 radius = 1.2000000000000000e+000 area = 4.521600 radius = 1.3999999999999999e+000 area = 6.154400 radius = 1.5999999999999999e+000 area = 8.038400 radius = 1.7999999999999998e+000 area = 10.173600 radius = 1.9999999999999998e+000 area = 12.560000 radius = 2.1999999999999997e+000 area = 15.197600 radius = 2.3999999999999999e+000 area = 18.086400 radius = 2.6000000000000001e+000 area = 21.226400 radius = 2.8000000000000003e+000 area = 24.617600view plaincopy to clipboardprint? 01.#include <iostream> 02.#include <iomanip> 03.#include <string> 04. 05.using namespace std; 06. 07.int main( ) { 08. 09. ios_base::fmtflags flags = cout.flags( ); 10. 11. double pi = 3.14285714; 12. 13. cout << "pi = " << setprecision(5) << pi << '\n'; 14. 15. cout.flags(flags); 16.} #include <iostream> #include <iomanip> #include <string>using namespace std;int main( ) {ios_base::fmtflags flags = cout.flags( );double pi = 3.14285714;cout << "pi = " << setprecision(5) << pi << '\n';cout.flags(flags); }pi = 3.1429view plaincopy to clipboardprint? 01.#include <iostream> 02.#include <iomanip> 03.#include <math.h> 04.using namespace std; 05.int main() 06.{ 07. double root2 = sqrt( 2.0 ); 08. int places; 09. 10. cout << setiosflags( ios::fixed) 11. << "Square root of 2 with precisions 0-9.\n" 12. << "Precision set by the " 13. << "precision member function:" << endl; 14. 15. for ( places = 0; places <= 9; places++ ) { 16. cout.precision( places ); 17. cout << root2 << '\n'; 18. } 19. 20. cout << "\nPrecision set by the " 21. << "setprecision manipulator:\n"; 22. 23. for ( places = 0; places <= 9; places++ ) 24. cout << setprecision( places ) << root2 << '\n'; 25. 26. return 0; 27.}
转载于:https://www.cnblogs.com/suanec/p/4354599.html
[转载] c++ cout 格式化输出浮点数、整数及格方法相关推荐
- c 语言的输出函数cout,详解C++ cout格式化输出完全攻略
写算法题的时候突然发现自己忘记基本的C++:cout格式化输出了,赶紧拉出以前的C++学习笔记重新看一看. 部分内容来自教程:C语言中文网(一个很棒的网站) 有时希望按照一定的格式进行输出,如按十六进 ...
- 有趣的搬砖工 No.2 cout格式化输出
下面的代码将cout格式化输出的用法都用了一遍. #include <iostream> #include <iomanip>//不要忘记包含此头文件 using namesp ...
- python右对齐格式化输出_Python中格式化输出的两种方法介绍
本篇文章给大家带来的内容是关于Python中格式化输出的两种方法介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 方式一:使用占位符 % 常用占位符:% s (s = string ...
- c++ 输出二进制_Python入门3print格式化输出的几种方法
接<Python入门2> print格式化输出的几种方法 ⒂格式化输出举例 [例] str_name="小明" num_age=15 print("我叫%s, ...
- Java格式化输出的四种方法
一.System.out.printf() Java SE5推出了C语言printf()风格的格式化输出功能. String str="Java"; double pi=3.14; ...
- C++ cout格式化输出
希望按照一定的格式进行输出,如按十六进制输出整数,输出浮点数时保留小数点后面两位,输出整数时按 6 个数字的宽度输出,宽度不足时左边补 0,等等.C++ 中的 cout 对象则使用流操作算子(你也可以 ...
- C++ cout格式化输出,精确控制小数点后位数
仰天地之正气,法古今之完人. --同济大学老校训 昨天做OJ遇到一题要求把结果保留两位小数输出.惊觉自己完全没有掌握该技能.因此特地去网上搜了一下,发现C++ 的标准输出流的格式化输出很有趣.正好作为 ...
- 格式化输出和整数所占字节
格式化输出: %d – 输出十进制整数 %c – 输出一个字符 %s – 输出一个字符串 %f – 输出一个小数 %p – 它表示以十六进制的形式(带小写的前缀)输出数据的地址 整数 Linux: s ...
- c语言函数参数类型格式化,格式化输出的几种方法 主要介绍format函数的用法
1 str自带函数格式化输出 rjust() ljust() center() zfill() zfill是补齐零 介绍: rjust(...) S.rjust(width[, fillchar]) ...
- RT-thread rt_kprintf()函数格式化输出浮点数
使用rt-thread的同学可能会发现,RT官方预留的打印功能rt_kprintf无法输出小数(不知道是不是全部版本都这样,我这里使用的是3.1.4的版本出现这种情况,使用MCU为stm32) 即使用 ...
最新文章
- C语言 条件编译详解
- 我明明只是在努力工作,却被同事说成是“卷王”!!!
- typedef的详细用法
- ajax回复留言,Ajax 留言板模拟
- 认识Linux系统服务(鸟哥18章)
- 吴恩达深度学习5.3练习_Sequence Models_Trigger word detection
- 《线性代数及其应用 第四版》习题1.4
- 计算机网络课程设计小型企业局域网的组建,计算机网络课程设计小型企业局域网的组建.doc...
- kettle Windows下载
- 如何使用mp3转换器将wav转换成mp3格式
- 印刷厂ERP系统源码
- PyTorch | torch.manual_seed(1)是什么意思?torch随机数manual_seed(1)有什么用?如何理解torch.manual_seed(1)
- socket的基本使用
- 01炼数成金TensorFlow基本概念
- 我的世界服务器无限重启怎么办,iPhoneX无限重启怎么办?iPhoneX无限重启解决一览...
- python文本编辑器_python最好的ide和文本编辑器
- Titan学习笔记-初识
- 实用技巧:Google 搜索打不开的解决方法【图文教程】
- 【必备软件】电化学工作站测试软件及解决方案
- 战略与团队:组织变革时(《腾讯方法》读后感 总结一)