Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn off scientific notation on float

Tags:

c++

I'm trying to display number in standard notation

for example:

float f = 1230000.76

turns out to be,

1.23e+006
like image 233
use753231 Avatar asked Jun 10 '11 03:06

use753231


2 Answers

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);
like image 183
MD Sayem Ahmed Avatar answered Sep 23 '22 06:09

MD Sayem Ahmed


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

you need to write:

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

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

like image 44
AvinashK Avatar answered Sep 23 '22 06:09

AvinashK