Setting "setprecision" correctly - c++

Hi although I do something like this
#include <iomanip>
...
std::cout<<"Numbers "<<std::setprecision(2)<<numb1<< " "<<std::setprecision(2)<<numb2<<" "<<std::setprecision(2)<<numb3<<" "<< std::setprecision(4)<<numb4<<std::endl;
I do get
Numbers 14 1.5e+02 0.0053 & 220
How can I really make set the precision per column to get a consistent format and not this mixture of precision(5) and scientific format?

Use the std::fixed manipulator too which will switch off any reversion to scientific notation:
std::cout << std::fixed /*<< as before from here*/
If you want to switch the scientific notation back on again, then introduce the manipulator std::scientific.
Reference; http://en.cppreference.com/w/cpp/io/manip/fixed

Related

Elegant solution to remove trailing 0's with precision set

Is there any elegant solution using the std C++ or Boost libraries to output a double to std::cout in a way that the following conditions are met:
scientific notation is disabled
the precision for the decimal part is 6
however, trailing 0's (for the decimal part) are not printed out
For example:
double d = 200000779998;
std::cout << `[something]` << d;
should print out exactly 200000779998. [something] should possibly be a noexcept combination of some existing manipulators.
This is not a solution to the problem:
std::cout << std::setprecision(6) << std::fixed << d;
because it prints out 200000779998.000000 with trailing 0's
Instead of using the fixed manipulator, you can try to use (abuse?) defaultfloat. As far as I understand, it chooses either fixed or scientific based on the ability to put the number within the specified precision. As a result you can set the precision to the number of digits of the integral part + the requested fractional precision (6 in your case).
double d = 200000779998;
std::cout << std::setprecision(integralDigits(d) + 6) << d << std::endl;
You can try it here.
Hard to prove a negative, but I would assume no.
The requirements are inconsistent with any normal use. Space efficiency dictates a binary format. 6 digits (decimal) of precision suggests a format intended for human readers, who can't churn through lots of data. And humans have no issue dealing with a consistent 6 digit format.
So, you're basically targeting a format that has no obvious audience, and that is why I would be surprised if there is support for that.

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.

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.

Turn off scientific notation on float

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

How to understand the following c++ code?

inpfile>>ch;
if(ch<16) outfile<<"0×0"<<std::hex<<setprecision(2)<<(int)ch<<" ";
what does std::hex<<setprecision(2) mean?
iostreams can be manipulated to achieve the desired formatting - this is done by what at first sight looks like outputting predefined values to them as shown in our subject line of code.
std::hex displays following integer values in base16.
setprecision sets the precision for display of following floating values.
Fur further info on manipulators, start here
This line is the same as:
char ch;
inpfile>>ch;
if(ch<16)
{
outfile << "0×0" // Prints "0x0" (Ox is the standard prefix for hex numbers)
/*outfile*/ << std::hex // Tells the stream to print the next number in hex format
/*outfile*/ << setprecision(2) // Does nothing. Presumably they were trying to indicate print min of 2 characters
/*outfile*/ << (int)ch // Covert you char to an integer (so the stream will print it in hex
/*outfile*/ << " "; // Add a space for good measure.
}
Rather than setprecision(2) what was probably intended was setw(2) << setfill('0')
what does std::hex<<setprecision(2) mean?
std::hex and std::setprecision are both so-called manipulators. Applied to a stream (done by outputting them) they manipulate the stream, usually to change the stream's formatting. In particular, std::hex manipulates the stream so that values are written in hexadecimal, and std::setprecision(x) manipulates it to output numbers with x digits.
(A rather popular manipulator which you might already know about is std::endl.)
As you can see, there are manipulators that take arguments and those that take none. Also, most (in principle all) manipulators are sticky, which means their manipulation of the stream lasts until it is explicitly changed. Here is an extensive discussion about this topic.
std::hex sets the output base to hexadecimal.
setprecision has no effect on this line since it affects floating point only.