Turn off scientific notation on float - c++

I'm trying to display number in standard notation
for example:
float f = 1230000.76
turns out to be,
1.23e+006

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.

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);

Related

Matching printf Formatting with iomanip

I have some old C code I'm trying to replicate the behavior of in C++. It uses the printf modifiers: "%06.02f".
I naively thought that iomanip was just as capable, and did:
cout << setfill('0') << setw(6) << setprecision(2)
When I try to output the test number 123.456, printf yields:
123.46
But cout yields:
1.2+e02
Is there anything I can do in iomanip to replicate this, or must I go back to using printf?
[Live Example]
Try std::fixed:
std::cout << std::fixed;
Sets the floatfield format flag for the str stream to fixed.
When floatfield is set to fixed, floating-point values are written using fixed-point notation: the value is represented with exactly as many digits in the decimal part as specified by the precision field (precision) and with no exponent part.
The three C format specifiers map to corresponding format setting in C++ IOStreams:
%f -> std::ios_base::fixed (fixed point notation) typically set using out << std::fixed.
%e -> std::ios_base::scientific (scientific notation) typically set using out << std::scientific.
%g -> the default setting, typically set using out.setf(std::fmtflags(), std::ios_base::floatfield) or with C++11 and later out << std::defaultfloat. The default formatting is trying to yield the "best" of the other formats assuming a fixed amount of digits to be used.
The precision, the width, and the fill character match the way you already stated.

use fixed precision without trailing zeros

I'm using setPrecision and fixed so I can set the precision AFTER the decimal point,
but I don't want to get something like this: 5.00000.
how can I take off the trailing zeros in an elegent way?
any specific way or should I perform string manipulations?
os << setprecision(5) << fixed << value;
Short Answer: Don't use fixed and instead set precision to whatever number you want + number of digits before decimal place.

c++ std::stream double values no scientific and no fixed count of decimals

The following code will print value of a and b:
double a = 3.0, b=1231231231233.0123456;
cout.setf(std::ios::fixed);
cout.unsetf(std::ios::scientific);
cout << a << endl << b << endl
The output is:
3.000000
1231231231233.012451
You can see that a is outputed with fixed 6 count of decimals.
But I want the output like this:
3
1231231231233.012451
How can i set flags only once, and output the above result.
The stream inserts 0s following the double because the stream's default precision for the output of floating-point values is 6. Unfortunately there is no straightforward way of checking if the double represents a whole number (so you could then only print the integral part). What you could do however is cast the value to an integer.
std::cout << static_cast<int>(a);
The default formatting for floating point numbers won't support the formats as requested. There are basically three settings you could use:
std::fixed which will use precision() digits after the decimal point.
std::scientific which will use scientific notation with precision() digits.
std::defaultfloat which will choose the shorter of the two forms.
(there is also std::hexfloat but that just formats the number in an form which is conveniently machine readable).
What you could do is to create you own std::num_put<char> facet which formats the value into a local buffer using std::fixed formatting an strips off trailing zero digits before sending the values one.

Set Precision for double

I am trying to set the two decimal numbers for double type data entered by the user, and I have the proper header file , but the result on the display is only integer, no decimal ?
I do really appreciate any help.
You would want to use the following format.
cout << setprecision(# of places past decimal) << fixed << varName << endl;
The fixed Input output manipulator is what tells it that you are setting the precision for the number of places after the decimal point.

double truncates at 7 characters of output

double fat = 0.2654654645486684646846865584656566554566556564654654899866223625564668186456564564664564;
cout<<fat<<endl;
results in:
0.265465
Should it be 7 charcters longer? I thought that a double could hold more than that?
I also get the same result of a "long double".
You're just seeing the default precision used by an iostream.
To improve things, use std::setprecision().
const int max_digits = std::numeric_limits<double>::digits10;
std::cout << std::setprecision(max_digits) << fat << std::endl;
Use std::setprecision(std::numeric_limits<double>::digits10) for maximum precision
std::cout << std::setprecision(std::numeric_limits<double>::digits10) << fat << std::endl;
There are two issues here:
you only get 7 significant figures because your cout stream is defaulting to a precision of 7, as the other answers state you can increase this to std::numeric_limits<double>::digits10
double can only store a fixed amount of precision anyway so most of the digits assigned to fat will be thrown away (on most machines you will get up to 15 significant figures into a double)
The problem is with cout, which defaults to a certain number of decimal places. You can set cout's precision and then add a fixed to give you the precision you need
cout.precision(15);
cout << fixed << fat << endl;
use cout.precision().
or, you can also use std::numeric_limits< double > and #include <limits> to get the maximum precision of a float or double..
see this SO post.