Greetings dcving,
Fixing this issue is quite simple. Let me expound.
By using setprecision, you asked for two places, and it printed two places. If instead you want two decimal places (positions to the right of the decimal point), you must first choose the fixed-point format. For example:
cout << setiosflags(ios::fixed) << setprecision(2) << x;
The default C++ stream behavior for setprecision(n) is to apply the specification to the entire number, not the fractional part. If this is not what you want, set the fixed-point format first. Furthermore, setiosflags sets the format flags specified by parameter mask. Here is a list of the flags and their description:
flag effect if set
ios_base::boolalpha input/output bool objects as alphabetic names (true, false).
ios_base::dec input/output integer in decimal base format.
ios_base::fixed output floating point values in fixed-point notation.
ios_base::hex input/output integer in hexadecimal base format.
ios_base::internal the output is filled at an internal point enlarging the output up to the field width.
ios_base::left the output is filled at the end enlarging the output up to the field width.
ios_base::oct input/output integer in octal base format.
ios_base::right the output is filled at the beginning enlarging the output up to the field width.
ios_base::scientific output floating-point values in scientific notation.
ios_base::showbase output integer values preceded by the numeric base.
ios_base::showpoint output floating-point values including always the decimal point.
ios_base::showpos output non-negative numeric preceded by a plus sign (+).
ios_base::skipws skip leading whitespaces on certain input operations.
ios_base::unitbuf flush output after each inserting operation.
ios_base::uppercase output uppercase letters replacing certain lowercase letters.
Hope this helps,
-
Stack
Overflow