What does that line means in c++ [duplicate] - c++

This question already has answers here:
Setprecision is Confusing
(3 answers)
Closed 9 years ago.
I have an homework in C++ in college and there is a line I couldn't understand at all:
cout << fixed << setprecision( 2 );
Can anyone help me to explain this line?

They are both stream manipulators. By using them with std::cout, you are (with fixed) setting it to use fixed floating-point notation and then with setprecision changing the number of decimals printed by cout. Try e.g.
double a = 0.123456789;
cout << a << fixed << a << setprecision(2) << a << endl;
To see what effect they have.

cout<<fixed<<setprecision(2)
This sets the standard output stream to a fixed-point notiation with precision of 2.

You are writing a real number fixed (float/double) to the standard output stream. setprecision sets the decimal precision to be used to format floating-point values on output operations.

Your fixed is most likely a double value.
cout means print to console.
fixed is what you will have printed. and setprecision is how many decimal points the precision will be rounded to.
so if fixed is
3.1967
you will see
3.2

Related

How to print out the number 78.125 with two digits after the decimal point as 78.13 in C++

My assignment involve printing out double numbers with 2 digits after the decimal point. In one of the example tests, I got the output is 78.125. My teacher taught me to use the setprecision function to print out the number
#include <iomanip>
...some code
cout << fixed << setprecision(2);
cout << x /*the number*/
Normally this would do just fine. However the example output is 78.13 while the output of my code is 78.12. I can't figure out anyway to do this.
The rounding behaviour of std::cout and std::setprecision is implementation defined.
C++11 clears up this mess by supplying a function called std::fesetround which you can call using
std::fesetround(FE_TONEAREST);
before your std::cout statement. See http://en.cppreference.com/w/cpp/numeric/fenv/feround for more details.

Leading zeros of a binary number doesn't get printed

I am writing a code which involves converting a decimal number to binary and store the binary number. I'm not able to store the leading zeros in some of the binary number e.g 001101011 and instead it prints and stores -> 1101011. Any help would be appreciated. thanks
With my mind reading powers I'm deducing that this will help you.
printf("%08x", number);
To the best of my knowledge there is no standard data type for binary numbers in c++. So i guess you are using integers to store the binary number. So to print the leading zeroes just use this .
std::cout << std::setw(5) << std::setfill('0') << binary_number << std::endl;
See http://www.daniweb.com/software-development/cpp/threads/114864/setw-and-setfill.

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.

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