解析C++ 浮点数的格式化输出
来源:本站原创|时间:2020-01-10|栏目:C语言|点击: 次
C++格式化输出浮点数
复制代码 代码如下:
#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+009
Displayed in scientific format:
1.234567e-003 1.946000e+009
Displayed in fixed format:
0.001235 1946000000.000000
复制代码 代码如下:
#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+003
复制代码 代码如下:
#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.617600
复制代码 代码如下:
#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.1429
复制代码 代码如下:
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
double root2 = sqrt( 2.0 );
int places;
cout << setiosflags( ios::fixed)
<< "Square root of 2 with precisions 0-9./n"
<< "Precision set by the "
<< "precision member function:" << endl;
for ( places = 0; places <= 9; places++ ) {
cout.precision( places );
cout << root2 << '/n';
}
cout << "/nPrecision set by the "
<< "setprecision manipulator:/n";
for ( places = 0; places <= 9; places++ )
cout << setprecision( places ) << root2 << '/n';
return 0;
}
您可能感兴趣的文章
- 04-02c语言没有round函数 round c语言
- 01-10数据结构课程设计- 解析最少换车次数的问题详解
- 01-10深入理解C++中常见的关键字含义
- 01-10使用C++实现全排列算法的方法详解
- 01-10深入解析最长公共子串
- 01-10c++中inline的用法分析
- 01-10用C++实现DBSCAN聚类算法
- 01-10全排列算法的非递归实现与递归实现的方法(C++)
- 01-10C++大数模板(推荐)
- 01-10浅谈C/C++中的static与extern关键字的使用详解
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-11ajax实现页面的局部加载
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-10C#中split用法实例总结
- 01-10delphi制作wav文件的方法
- 04-02jquery与jsp,用jquery
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10SublimeText编译C开发环境设置
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文