How to stop doubles converting to scientific notation when using a stringstream

ⅰ亾dé卋堺 提交于 2019-11-28 10:06:07

Use std::fixed stream manipulator as:

ss << fixed << numb;

--

Example,

#include <iostream>
using namespace std;

int main () {
  double a,b,c;
  a = 3.1415926534;
  b = 2006.0;
  c = 1.0e-10;
  cout.precision(5);
  cout       <<         a << '\t' << b << '\t' << c << endl;
  cout <<   fixed    << a << '\t' << b << '\t' << c << endl;
  cout << scientific << a << '\t' << b << '\t' << c << endl;
  return 0;
}

Output:

3.1416          2006            1e-010
3.14159         2006.00000      0.00000
3.14159e+000    2.00600e+003    1.00000e-010

Example is taken from here.

And you can use std::stringstream instead of cout, but the result would be same. Experiment it here:

http://www.ideone.com/HUrRw

You need to use stream manipulators to format the string as you want it. In your case, you will probably want to use the fixed format flag :

ss << std::fixed << numb;

The opposite (if you ever want to force scientific notation) is the scientific format flag :

ss << std::scientific << numb;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!