服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 解析C++ 浮点数的格式化输出

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

2020-12-14 14:36C++教程网 C/C++

本篇文章是对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;
}

延伸 · 阅读

精彩推荐
  • C/C++c/c++实现获取域名的IP地址

    c/c++实现获取域名的IP地址

    本文给大家汇总介绍了使用c/c++实现获取域名的IP地址的几种方法以及这些方法的核心函数gethostbyname的详细用法,非常的实用,有需要的小伙伴可以参考下...

    C++教程网10262021-03-16
  • C/C++c/c++内存分配大小实例讲解

    c/c++内存分配大小实例讲解

    在本篇文章里小编给大家整理了一篇关于c/c++内存分配大小实例讲解内容,有需要的朋友们可以跟着学习参考下。...

    jihite5172022-02-22
  • C/C++使用C++制作简单的web服务器(续)

    使用C++制作简单的web服务器(续)

    本文承接上文《使用C++制作简单的web服务器》,把web服务器做的功能稍微强大些,主要增加的功能是从文件中读取网页并返回给客户端,而不是把网页代码...

    C++教程网5492021-02-22
  • C/C++深入C++拷贝构造函数的总结详解

    深入C++拷贝构造函数的总结详解

    本篇文章是对C++中拷贝构造函数进行了总结与介绍。需要的朋友参考下...

    C++教程网5182020-11-30
  • C/C++C语言main函数的三种形式实例详解

    C语言main函数的三种形式实例详解

    这篇文章主要介绍了 C语言main函数的三种形式实例详解的相关资料,需要的朋友可以参考下...

    ieearth6912021-05-16
  • C/C++关于C语言中E-R图的详解

    关于C语言中E-R图的详解

    今天小编就为大家分享一篇关于关于C语言中E-R图的详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看...

    Struggler095962021-07-12
  • C/C++OpenCV实现拼接图像的简单方法

    OpenCV实现拼接图像的简单方法

    这篇文章主要为大家详细介绍了OpenCV实现拼接图像的简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    iteye_183805102021-07-29
  • C/C++C语言实现双人五子棋游戏

    C语言实现双人五子棋游戏

    这篇文章主要为大家详细介绍了C语言实现双人五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    两片空白7312021-11-12