解析C++ 浮点数的格式化输出

所属分类: 软件编程 / C 语言 阅读数: 54
收藏 0 赞 0 分享
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;
}

更多精彩内容其他人还在看

visual studio 2019编译c++17的方法

这篇文章主要介绍了visual studio 2019编译c++17的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Qt串口通信开发之QSerialPort模块详细使用方法与实例

这篇文章主要介绍了Qt串口通信开发之QSerialPort模块详细使用方法与实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Qt串口通信开发之Qt串口通信模块QSerialPort开发完整实例(串口助手开发)

这篇文章主要介绍了Qt串口通信开发之Qt串口通信模块QSerialPort开发完整实例(串口助手开发),需要的朋友可以参考下
收藏 0 赞 0 分享

Qt串口通信开发之QSerialPort模块简单使用方法与实例

这篇文章主要介绍了Qt串口通信开发之QSerialPort模块简单使用方法与实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Qt串口通信开发之QSerialPort模块Qt串口通信接收数据不完整的解决方法

这篇文章主要介绍了Qt串口通信开发之QSerialPort模块Qt串口通信接收数据不完整的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Qt图形图像开发之Qt曲线图美化QChart QScatterSeries 空心点阵图,鼠标移动到上面显示数值,鼠标移开数值消失效果实例

这篇文章主要介绍了Qt图形图像开发之Qt曲线图美化QChart QScatterSeries 空心点阵图,鼠标移动到上面显示数值,鼠标移开数值消失效果实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Qt GUI图形图像开发之QT表格控件QTableView,QTableWidget复杂表头(多行表头) 及冻结、固定特定的行的详细方法与实例

这篇文章主要介绍了Qt GUI图形图像开发之QT表格控件QTableView,QTableWidget复杂表头(多行表头) 及冻结、固定特定的行的详细方法与实例,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言实现加密解密功能

这篇文章主要为大家详细介绍了C语言实现加密解密功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C++实现猴子吃桃的示例代码

这篇文章主要介绍了C++实现猴子吃桃的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C语言实现关机小程序

这篇文章主要为大家详细介绍了C语言实现关机小程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多