15

I'm trying to display number in standard notation

for example:

float f = 1230000.76

turns out to be,

1.23e+006
use753231
  • 163
  • 1
  • 1
  • 5

2 Answers2

14

There are two things found in iomanip that must be included. First is fixed and the second is setprecision

You need to write:

std::cout << fixed;  
std::cout << setprecision(2) << f;

fixed disables the scientific notation i.e. 1.23e+006 and fixed is a sticky manipulator so you need to disable it if you want to revert back to scientific notation.

user438383
  • 5,716
  • 8
  • 28
  • 43
AvinashK
  • 3,309
  • 8
  • 43
  • 94
8

Use -

cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);

before printing out the floating point numbers.

More information can be found here.

You can also set output precision with the following statement -

cout.precision(2);

or simply with -

printf("%.2f", myfloat);
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
  • 1
    Ok, it came up as `1230000.750000` can it be a bit more accurate? – use753231 Jun 10 '11 at 03:11
  • You can set the precision using either with cout.precision(ACCURACY_THAT_YOU_WANT) before printing the floating point numbers or with printf("%.ACCURACY_THAT_YOU_WANTf", myFloat) – MD Sayem Ahmed Jun 10 '11 at 03:13
  • 2
    For most C/C++ environments, a `float` has 6 to 7 significant figures of precision. Displaying it beyond that does not increase the accuracy. If you want more precision in handling the value, change the datatype to `double`, which has about 15 significant figures of precision, or `long double` with about 19. – wallyk Jun 10 '11 at 03:23
  • @use753231: I agree with wallyk, and there is a slight mistake that I made in my previous comment. I should have written PRECISION in place of ACCURACY. If float's accuracy doesn't work for you, then try changing the datatype to double or long double just like wallyk suggested. – MD Sayem Ahmed Jun 10 '11 at 03:32
  • How to achieve this if I'm using a file pointer? As in , ` fp << (float) x << endl ;` ? – saruftw Feb 07 '17 at 07:42